ASP.NET Core - Search for GET requests

Asked

Viewed 809 times

-1

I am using ASP.NET Core 2.1 Web API and need to perform searches by certain type of field, IE, on my front I can choose the field I want to do the research and my back-end need to pick this 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 only by Name. How do I get the search data from this method?

  • I don’t know if I understood your doubt as being easier than it really is, but this link here helps you? If you scroll a little, you will see that it teaches you how to make GET request, among other things.

  • Unfortunately it doesn’t help me. I wonder how I receive the parameters dynamically from my application front.

  • Show the code, only with this information it is difficult to understand the scenario. But from what I could understand, you could use Viewmodels to reflect and make the validations and queries you want. Or even a more complex structure to implement the search route in your Controllers... depedende muito do cenário e do que você realmente deseja.

1 answer

0


You need to create a controller for your domain object:

Apiversion = Version of your API

Route = Route to access your controller by url

Httpget = You define that your method will be the GET verb of the HTTP request. The parameter that I pass to the attribute ("searchFotosMesCurrent/{code}") defines which url will be used to make the request. The keys are used to send a parameter to the server, in this case I am sending the installation code that is a string (because I am not setting the type). If you want to pass an integer parameter, you can pass as follows {identifier:int}. The name of the parameter in the HTTPGET attribute must be the same as in 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 reported in your question).

To call this method would be as follows

http:localhost:5000/processamento/Foto/buscarFotoMesCorrente/AI1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.