Reputation: 490
I need to be able to have the same route to get two different kinds of output.
/api/v1/items?ids=1,2,3
should retrieve the list of items containing those three entries, however
/api/v1/items?from=142523&limit=4
should retrieve a cursor paginated response, where from
would be the id of the item.
I know that in the past it was possible to create route constraints based on querystring, but that has been removed according to the answer posted here: Query parameter route constraints
What would be the best way to tackle this? Attribute routing is a no-go, as we don't want to have a items/{list-of-ids}
routes in the application. I could merge the methods into one with optional parameters, but then the API output is inconsistent in the automatically generated documentation (paginated vs non-paginated response). Is it possible to achieve what I want using custom route constraints?
Upvotes: 0
Views: 200
Reputation: 77926
You can use Optional Parameter
to define your API endpoint like below. Your query string values will automatically get parameter binded if you keep the parameter name same and don't have to define separate route for them
[HttpGet]
public IActionResult items(string ids, int from = 0, int limit = 0)
{
//code here
}
Upvotes: 2