In ASP.NET Core Minimal APIs, why does a MapGet handler with an unregistered RecipeFilter class parameter fail, and in what order is a parameter's source inferred?
answer
- no inferred body on GET or DELETE
- "Body was inferred but the method does not allow"
- thrown when the endpoint is built
- attributes, special types, BindAsync, TryParse, DI, body
- only one body parameter per handler
basics
~20 sAn unregistered class that has no BindAsync or TryParse is inferred as the JSON body, and GET, DELETE, HEAD, OPTIONS, TRACE and CONNECT endpoints do not allow an inferred body, so building the endpoint throws InvalidOperationException.
solid answer
~40 sMinimal API inference runs in a fixed order: an explicit attribute (`[FromRoute]`, `[FromQuery]`, `[FromHeader]`, `[FromBody]`, `[FromForm]`, `[FromServices]`, `[FromKeyedServices]`, `[AsParameters]`), then the special types (`HttpContext`, `CancellationToken` and so on), then a static `BindAsync`, then `string` or a `TryParse` type from route or query, then a registered service, and finally the body. A `RecipeFilter` class with none of those falls through to the body. When the endpoint is mapped with GET, DELETE, HEAD, OPTIONS, TRACE or CONNECT, ASP.NET Core sets body inference off and throws `InvalidOperationException` ("Body was inferred but the method does not allow inferred body parameters") while building the endpoint, listing every parameter and its inferred source. The fix is `[AsParameters] RecipeFilter filter`, a `BindAsync` on the type, or `[FromBody]` if a GET body is truly intended.
code
csharp · 13 lines// Throws InvalidOperationException when the endpoint is built:
// "Body was inferred but the method does not allow inferred body parameters."
app.MapGet("/recipes", (RecipeFilter filter, IRecipeStore store) => store.Search(filter));
// Works: properties are inferred one by one (query string here)
app.MapGet("/recipes", ([AsParameters] RecipeFilter filter, IRecipeStore store) =>
store.Search(filter));
// GET: tags from ?tags=vegan&tags=quick
app.MapGet("/recipes/tagged", (string[] tags, IRecipeStore store) => store.ByTags(tags));
// POST: the same signature reads tags from the JSON body
app.MapPost("/recipes/tagged", (string[] tags, IRecipeStore store) => store.ByTags(tags));go deeper
Remember that a MapGet handler cannot take a plain class parameter and expect query values in it; it needs [AsParameters] or separate parameters.
Recite the order (attributes, special types, BindAsync, TryParse, DI, body) and explain why GET-family endpoints turn off the last step.
Read the build-time source table quickly, choose between [AsParameters], BindAsync and explicit parameters, and spot that a DI registration only hides the bug.
Decide whether a codebase relies on inference or requires explicit sources on public endpoints, and how reviews catch a signature that changes binding.
## The failure in one picture A recipe-sharing API wants a search endpoint: ```csharp public class RecipeFilter { public string? Cuisine { get; set; } public int MaxMinutes { get; set; } } app.MapGet("/recipes", (RecipeFilter filter, IRecipeStore store) => store.Search(filter)); ``` The expectation, carried over from MVC controllers, is that `?cuisine=thai&maxMinutes=30` fills the object. Instead the app throws an `InvalidOperationException` whose message starts with **"Body was inferred but the method does not allow inferred body parameters."**, followed by a table of every parameter and the source ASP.NET Core inferred for it, and ends with "Did you mean to register the "Body (Inferred)" parameter(s) as a Service or apply the [FromServices] or [FromBody] attribute?". The exception is raised when ASP.NET Core builds the endpoint's request delegate, which happens once when routing first materializes the app's endpoints, not as a per-request 400, so no client ever gets a half-bound filter. ## The inference order `RequestDelegateFactory`, which builds Minimal API request delegates, walks each parameter through these checks and stops at the first that applies: | Step | Condition | Source | |---|---|---| | 1 | an explicit attribute: `[FromRoute]`, `[FromQuery]`, `[FromHeader]`, `[FromBody]`, `[FromForm]`, `[FromServices]`, `[FromKeyedServices]`, `[AsParameters]` | what the attribute says | | 2 | a special type: `HttpContext`, `HttpRequest`, `HttpResponse`, `ClaimsPrincipal`, `CancellationToken`, `IFormCollection`, `IFormFileCollection`, `IFormFile`, `Stream`, `PipeReader` | the request itself | | 3 | the type has a static `BindAsync` | the type's own code | | 4 | `string` or a type with a static `TryParse` | route if the name is in the template, else query | | 5 | body inference is off and the type is `string[]`, `StringValues` or an array of a `TryParse` type | query string | | 6 | `IServiceProviderIsService` reports a registered service | DI | | 7 | anything else | the JSON body | `RecipeFilter` fails steps 1 to 6, so it lands on step 7. ## Which methods switch body inference off When an endpoint is mapped, the HTTP methods it accepts decide whether step 7 is allowed: - **GET, DELETE, HEAD, OPTIONS, TRACE and CONNECT** turn body inference off. If any of them is in the endpoint's method list, the whole endpoint is treated as body-less, which matters for `MapMethods` with GET and POST together. - **POST, PUT and PATCH** allow it. - **`Map`** with no method list allows it, because the endpoint might receive any method. The same switch explains a quieter difference. On GET, a `string[] tags` or `int[] ids` parameter is bound from repeated query values (step 5, `?tags=vegan&tags=quick`). On POST the same signature skips step 5 and is read from the JSON body. ## The other build-time errors Inference errors show up as exceptions while the endpoints are built rather than as odd runtime data: 1. **Two inferred body parameters** (two DTOs on one `MapPost`) throw "Failure to infer one or more parameters." with the same source table, suggesting the unknown one should be a service. 2. **A JSON body together with form parameters** on one handler is rejected. 3. **A `TryParse` or `BindAsync` with the wrong signature** throws and prints the signatures it expected. ## Fixing the search endpoint - **`[AsParameters] RecipeFilter filter`** flattens the class: each settable property is inferred like a handler parameter, so `Cuisine` and `MaxMinutes` come from the query string. - **A static `BindAsync(HttpContext)`** on `RecipeFilter` lets the type read whatever it needs, including several query keys and headers, at step 3. - **Individual parameters** (`string? cuisine, int? maxMinutes`) are the simplest choice for two or three values. - **`[FromBody]`** forces a body on GET; it compiles and runs, but many clients and proxies do not send bodies on GET, so it is rarely what a public API wants. Registering `RecipeFilter` as a service would also silence the error, and would be wrong: the handler would receive an empty object from DI. ## What interviewers listen for - That the order is fixed and **type-driven**, ending at the body. - That GET-family endpoints **refuse** an inferred body, and that the failure is an **exception at endpoint build time**, not a 400. - That arrays bind from the query on GET but from the body on POST. - A fix that uses `[AsParameters]` or `BindAsync` rather than registering the DTO in DI.
- Why not register RecipeFilter in DI to make the error go away?Because step 6 would then win: `IServiceProviderIsService` reports the type as a service, so the handler receives whatever the container builds, an empty `RecipeFilter`, and the query string is ignored. The startup error disappears and the endpoint silently stops filtering. The error is telling you the type has no binding rule, so give it one with `[AsParameters]` or `BindAsync`.
- A handler is mapped with MapMethods for both GET and POST. Can it infer a body?No. ASP.NET Core checks the endpoint's method list and turns body inference off if any method in it is GET, DELETE, HEAD, OPTIONS, TRACE or CONNECT. A complex parameter on that endpoint therefore throws at build time, and you either split the endpoint in two or mark the parameter `[FromBody]` explicitly.
- A MapGet handler fills a plain class parameter from matching query-string keys.
- An inference mistake shows up as a 400 for the client, not as an exception at build time.
- Registering the DTO as a service is the correct fix for an inferred-body error.
- A string[] parameter is always read from repeated query values, whatever the HTTP method.
- A handler may declare two inferred body parameters and ASP.NET Core splits the JSON between them.