In ASP.NET Core Minimal APIs, what does [AsParameters] do to a handler parameter, and which shapes of type can it not bind?
answer
- flattens a type into parameters
- each property inferred on its own
- constructor parameters match properties
- no nesting, no nullable, no abstract
- property initializer is not a default
basics
~10 s[AsParameters] treats a class, struct or record as a list of handler parameters: each constructor parameter or settable property is inferred like a handler parameter. It cannot be nested, nullable or abstract.
solid answer
~50 s`[AsParameters]` (namespace `Microsoft.AspNetCore.Http`) tells the Minimal API binder to expand a type into its members and bind each one as if it were its own handler parameter. If the type has a single public parameterized constructor whose parameters match its public properties, as a positional record does, ASP.NET Core calls it; otherwise it uses the parameterless constructor and sets every public settable property. Each member keeps the normal rules, so a property can be a route value, a query value, a header with `[FromHeader]`, a DI service or a `CancellationToken`. It is not model binding: nesting `[AsParameters]` throws `NotSupportedException`, and a nullable or abstract type throws `InvalidOperationException`. With nullable reference types on, a non-nullable member is required unless its constructor parameter has a default, so an initializer like `= 20` does not make it optional.
code
csharp · 13 lines// Settable-property shape: parameterless constructor + public setters
public class RecipePaging
{
public int? Page { get; set; } // optional: nullable
public int PageSize { get; set; } = 20; // REQUIRED: an initializer is not a default
[FromHeader(Name = "X-Diet")]
public string? Diet { get; set; } // optional header
}
app.MapGet("/recipes", ([AsParameters] RecipePaging p, IRecipeStore store) =>
store.Page(p.Page ?? 1, p.PageSize, p.Diet));
// GET /recipes?page=2 -> 400: required parameter pageSize was not providedgo deeper
Know that [AsParameters] groups several handler parameters into one type and that each member still binds from route, query, header or DI as usual.
Explain the constructor-versus-setter strategy, why positional records fit, and the nested, nullable and abstract rejections.
Catch the initializer-is-not-a-default trap in review and judge when a shared paging type is worth the indirection it adds to signatures.
Decide whether shared request types like paging become a house convention and how their member names are kept stable as a public contract.
## Why the attribute exists A paged recipe search reads a handful of values from different places: the cuisine from the route, the search text and paging from the query string, a dietary preference from a header, plus the recipe store and the request's `CancellationToken`. Written out, the handler signature grows to six or seven parameters, and every endpoint that pages repeats them. `AsParametersAttribute` lets you move that list into a type: ```csharp readonly record struct RecipeSearch( string Cuisine, // route {cuisine} IRecipeStore Store, // DI CancellationToken Ct, // RequestAborted string? Q, // query ?q= int Page = 1, // query, optional int PageSize = 20, // query, optional [FromHeader(Name = "X-Diet")] string? Diet = null); // header app.MapGet("/recipes/{cuisine}", ([AsParameters] RecipeSearch s) => s.Store.SearchAsync(s.Cuisine, s.Q, s.Page, s.PageSize, s.Diet, s.Ct)); ``` The endpoint behaves exactly as if the seven values were declared on the lambda. The attribute does not change *where* values come from; it changes only how they are packaged. ## How the type is constructed `RequestDelegateFactory` picks a construction strategy once, when the endpoint is built: 1. **A single public parameterized constructor** whose parameters all match public properties (the shape a positional `record` or `record struct` has) is called with one bound argument per parameter. Defaults and attributes on the constructor parameters apply. 2. Otherwise the **public parameterless constructor** runs and every **public property with a public setter** is assigned a bound value. 3. A class with **several public constructors and no parameterless one**, or a constructor parameter that matches no public property by name and type, is rejected with an `InvalidOperationException` that names the problem. The docs note that a `struct` can be cheaper than a `record` class here, since nothing is allocated on the heap for the wrapper. ## What it cannot do | Shape | Result | |---|---| | `[AsParameters]` on a property of an `[AsParameters]` type | `NotSupportedException`: nesting is not supported | | `[AsParameters] RecipeSearch? s` | `InvalidOperationException`: mark the parameter non-nullable | | an abstract type | `InvalidOperationException`: abstract types are not supported | | a property whose type is a plain class | inferred as the body, so on GET it throws like any other inferred body | It is also not MVC model binding. There is no prefix matching (`?search.page=2`), no `ModelState`, and MVC attributes such as `[BindRequired]` or `[BindNever]` have no effect. ## Required and optional members Optionality follows the member, not the wrapper: - A **nullable** member (`string?`, `int?`) is optional. - A **non-nullable** member is optional only when its **constructor parameter has a default value**, as `Page = 1` above. - A settable property with an **initializer** (`public int PageSize { get; set; } = 20;`) has no default from the binder's point of view. It is required, and a request without `pageSize` gets 400. Declare it `int?` or move the default into a constructor parameter. ## Effects elsewhere - Each expanded member is recorded in endpoint metadata as its own parameter (`ParameterBindingMetadata`), so tools that describe the endpoint see `cuisine`, `q`, `page`, `pageSize` and `X-Diet` rather than one `s` parameter. - The member names are the binding names: renaming `Q` to `Query` changes the query key a client must send. ## When to reach for it - **Repeated parameter groups.** Paging, sorting and tenant headers that appear on many recipe endpoints become one type, so a new endpoint cannot forget `pageSize` or spell it differently. - **Long signatures.** Once a handler takes six or more values, a named type is easier to read and review than a positional lambda. - **Not for custom parsing.** If a value needs logic to assemble (two query keys combined, a header with a fallback), `BindAsync` on a dedicated type is the right hook; `[AsParameters]` only regroups ordinary inference. - **Not for validation.** It adds no checks of its own; range and required-field rules come from the validation features layered on top. ## What interviewers listen for - That `[AsParameters]` **flattens**, and every member is inferred with the ordinary rules. - The **constructor-versus-setter** rule and why a positional record is the natural fit. - The three rejections: **nested, nullable, abstract**. - The initializer trap, which turns an intended default into a 400.
- Can an [AsParameters] type carry the request body as one of its members?Yes. A member is inferred like any handler parameter, so a complex-typed property on a POST endpoint becomes the JSON body, or you can mark it `[FromBody]`. The one-body rule still applies across the whole handler: a body member plus another body parameter on the lambda fails when the endpoint is built.
- Why might a team prefer a record struct over a class for [AsParameters]?A positional `record struct` uses the constructor path, so defaults written on its parameters make members optional, its members are immutable once bound, and the wrapper is a value type, which the docs say can perform better than a `record` class. The settable-class shape works too, but it invites the initializer trap.
- [AsParameters] binds a complex type from query keys with MVC-style prefixes such as search.page.
- A property initializer like = 20 makes an [AsParameters] property optional.
- [AsParameters] types can be nested to group related parameters.
- Declaring [AsParameters] RecipeSearch? lets the whole group be absent.
- [BindRequired] on an [AsParameters] property makes it required.