liveBook · Manning

AsParametersAttribute

As explained in ASP.NET Core in Action, 3e

AsParametersAttribute is an ASP.NET Core attribute that expands a parameter object's properties or constructor parameters into endpoint parameters bound from multiple request sources.

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains wrapper types, constructor or property binding, and explicit sources, but defers detailed binding documentation.
Examples
Uses a category-search endpoint combining route, header, and query values in a SearchModel record struct.
What it assumes
Assumes basic endpoint parameters and request binding while teaching parameter encapsulation from a concrete minimal API example.
What you can do after
Apply it via ASP.NET Core minimal APIs to make endpoint signatures more readable without changing individual binding rules.
How it connects
Connects minimal APIs with model binding, binding-source attributes, dependency injection, custom binding, and validation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.