Minimal API query & route parameter mapping to an object
See original GitHub issueSummary
The minimal API’s currently look for a public static bool TryParse(...)
method to map parameters from query or route parameters to an object. It would be great to have this look at the properties on an object to match route and/or route parameters.
Motivation and goals
A get request on a minimal API route that takes a set of parameters where it may be nicer to map these to an object rather than a set of arguments on a delegate.
In scope
- Map query parameters to properties on an object for get & delete requests.
- Map route parameters to properties on an object for get & delete requests.
Out of scope
- Supporting post & put requests.
Risks / unknowns
- It may add more complexity to the by design set of minimal apis.
Examples
Query String Match
app.MapGet("api/todo", (FetchTodosQuery query) => {
return Results.Ok($"Page: {query.Page} and size {query.PageSize}");
});
public class FetchTodosQuery
{
public int Page { get; set; }
public int PageSize { get; set; }
}
The above route would map from a query string such as this http://localhost:5001/api/todo?page=1&pageSize=10
Route Match
app.MapGet("api/todo/{page}/{pageSize}", (FetchTodosQuery query) => {
return Results.Ok($"Page: {query.Page} and size {query.PageSize}");
});
public class FetchTodosQuery
{
public int Page { get; set; }
public int PageSize { get; set; }
}
The above route would map from a query string such as this http://localhost:5001/api/todo/1/10
Implicit vs Explicit
I would expect the choice of which method to use could always be overriden using the attributes [FromRoute]
& [FromQuery]
.
Detailed design
I am pretty sure this is already supported in controllers for aspnet core when using the [FromQuery]
attribute it will build up a dictionary of values from the query string and map these where possible to properties on an object.
I would expect this to work the same.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Closing in favor of https://github.com/dotnet/aspnetcore/issues/35489
Okay that is great thank you, certainly will let me move forward with my idea. Will this issue continue to track the changes that will come as part of .NET 7 or will I need to keep my 👁️ out?