How to receive querystring value in an action?

Asked

Viewed 33 times

1

I am creating an API with ASP.NET Core and would like to know how to do a search via querystring, in case the URL sees something like /api/programas?nome=teste

Like I would in my controller?

Example of my controller, in this case filtering by ID (just an example showing how I’m doing my endpoint):

public async Task<ActionResult<AppResponse>> GetAsync(Guid id)
{
    var app = await _programaHandle.GetAsync(id);
                     
    return Ok(app);
}

1 answer

2


It is possible to declare a parameter in the action and use the attribute FromQuery.

public async Task<ActionResult<AppResponse>> GetAsync(Guid id, [FromQuery] string nome)
{
    var app = await _programaHandle.GetAsync(id);                 
    return Ok(app);
}
  • blz, only one other question, if it is several filters, like -> /api/programs? name=test&&category=test&&app=test, I need to pass - > [Fromquery] string name, application string, category string, or there is a way to get 1 parameter already for everything, in case a...

  • pq this will be optional, one will be able to pass 1 filter, 2 filters, 3 filters, as many as she wants...

  • @Desalex You can create a class with all properties and ask for an instance of it as an action parameter. In this case, you use the FromQuery only in the same parameter.

  • just to illustrate, assuming that I have an Appresponse class, I would pass [Fromquery] Appresponse, or only Appresponse??

  • First option.

Browser other questions tagged

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