ASP.NET Core - Search for GET requests [closed]

-1

I'm using ASP.NET Core 2.1 Web API and I need to perform searches for a certain type of field, ie on my front I can choose the field I want to do the search and my back end needs to get that value.

[HttpGet]
public ActionResult<ResponseResult> GetAll()
{
    return _materialHandler.Handle(new GetAllMaterialCommand());
}

In this example, I have the method of my controller, in which I need to receive parameters dynamically. Example: I need to search the client by Name and CPF or by name only. How do I get the search data in this method?

    
asked by anonymous 30.07.2018 / 13:43

1 answer

0

You need to create a controller for your domain object:

ApiVersion = Your API Version

Route = Route to access your controller via the url

HttpGet = You define that your method will be the GET verb of the HTTP request. The parameter that I pass to the attribute ("searchFotosMesCorrente / {code}") defines which url will be used to make the request. The keys are used to send a parameter to the server, in which case I am sending the installation code which is a string (since I am not defining the type). If you want to pass an integer parameter, you can pass as {identifier: int}. The parameter name in the HTTPGET attribute should be the same as the method parameter.

using using Microsoft.AspNetCore.Mvc;

[ApiVersion("1.0")]
[Route("processamento/[controller]")]
public class FotoController : Controller
{
    [HttpGet("buscarFotosMesCorrente/{codigo}")]
    public async Task<IActionResult> BuscarFotosMesCorrente(string codigo)
    {
        var idEmpresa = base.IdEmpresa.Value;

        var item = await _service.BuscarFotosMesCorrente(idEmpresa, codigo);

        if (item == null)
            return NotFound();

        return Ok(item);
    }
}

In the body of the method you implement the logic you want to perform in the call of this method, such as fetching the object in the database (as you entered in your question).

To call this method would look like this

http:localhost:5000/processamento/Foto/buscarFotoMesCorrente/AI1
    
30.07.2018 / 14:57