AsParametersAttribute groups an endpoint handler’s route, header, query, body, service, or well-known parameters into one class, record, or struct, making the method signature easier to read. section 7.9
Grouped search parameters
app.MapGet("/category/{id}", ([AsParameters] SearchModel model) => $"Received {model}"); record struct SearchModel( int id, int page, [FromHeader(Name = "sort")] bool? sortAsc, [FromQuery(Name = "q")] string search); section 7.9
Use the same binding rules as ordinary endpoint parameters:
- Choose a class, record, struct, or record struct; define properties or constructor parameters. section 7.9
- Apply [FromHeader], [FromQuery], [FromBody], or service injection when explicit binding sources are needed. section 7.9
Listing 7.9 Using [AsParameters] to simplify endpoint handler parameters
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();
app.MapGet("/category/{id}",
([AsParameters] SearchModel model) => $"Received {model}"); #1
app.Run();
record struct SearchModel(
int id, #2
int page, #2
[FromHeader(Name = "sort")] bool? sortAsc, #2
[FromQuery(Name = "q")] string search); #2
Browse the complete minimal API example showing a record struct that combines route, header, and query values. section 7.9
