الانتقال إلى المحتوى
.NETMinimal APIs· 6 دقائق للقراءة

Where Did That Parameter Come From? Parameter Binding in Minimal APIs

Minimal APIs choose each parameter's source from a fixed list of rules. Where those rules surprise you, and when FromHeader or AsParameters changes the result.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • .NET 10
محتويات المقال

A teammate renames a route parameter from {id} to {orderId} because it reads better in the OpenAPI document. The handler still says int id. The build is clean, no analyzer complains, and in production GET /orders/42 starts answering 400 with an empty body. At the default log level, nothing is logged. The parameter didn't vanish: it quietly moved from the route to the query string, where nobody sends it.

The common misconception

The belief: minimal APIs look at each handler and figure out where every parameter should come from. Attributes like [FromQuery], [FromBody] and [AsParameters] are there for readability or documentation, and leaving them out changes nothing.

Most of the time, that's exactly what you see. (int id) with a route of /orders/{id} reads the route. (Order order) on a POST reads the JSON body. A registered service arrives from without any attribute. The official samples rarely use attributes, so it's easy to conclude the framework is smart enough to always work it out.

But in minimal APIs isn't inference in the "figure out what you meant" sense. It's a short, ordered list of rules, and the first rule that matches wins.

What actually happens

The rules, in order

For each parameter, ASP.NET Core takes the first source that applies:

  1. An explicit attribute: [FromRoute], [FromQuery], [FromHeader], [FromBody], [FromForm], [FromServices] or [AsParameters].
  2. A special type: HttpContext, HttpRequest, HttpResponse, ClaimsPrincipal, CancellationToken, a form or file type, Stream or PipeReader.
  3. A type with a static BindAsync method.
  4. A string, or a type with a static TryParse method such as int or Guid: from the route if its name appears in the route template, otherwise from the query string.
  5. A type registered as a service: from dependency injection.
  6. Anything else: from the request body.

Three consequences of this list surprise people.

The name is the contract

Rule 4 compares the parameter's name with the route template, and nothing else. Rename {id} to {orderId} and int id no longer matches, so it's read from ?id= instead. Parameters are required unless they're nullable or have a default value, so a request without ?id= fails with "Required parameter "int id" was not provided from query string." In Development, that's a BadHttpRequestException on the developer exception page. In other environments, ThrowOnBadRequest is off: the client gets a 400 with an empty body and the reason is logged only at Debug level.

Headers are never inferred

No rule reads a header unless you ask for it. A parameter string apiKey meant for the X-Api-Key header is bound from ?apiKey=. Clients that send the header get a 400, and the "fix" someone eventually finds is to put the key in the URL, where it ends up in access logs and browser history.

Complex types fall through to the body

A record such as OrderQuery(string? Status, int Page) has no TryParse and isn't a service, so it lands on rule 6: the body. But GET, HEAD, OPTIONS and DELETE don't bind from the body implicitly, so the endpoint can't be built: "Body was inferred but the method does not allow inferred body parameters." A service you forgot to register falls through the same way, because it's just a type the container doesn't know.

Controller habits make this one worse. In MVC, model binding fills a complex object property by property from the query string. Minimal APIs don't do that for a plain parameter; [AsParameters] is the opt-in.

The orders endpoints, fixed

The same three endpoints, first relying on the rules by accident, then saying what they mean:

C# Program.cs
var builder = WebApplication.CreateBuilder(args);var app = builder.Build();// {orderId} in the route, id in the handler: id is read from ?id=app.MapGet("/orders/{orderId:int}", (int id) => $"Order {id}");// headers are never inferred: apiKey is read from ?apiKey=app.MapGet("/reports", (string apiKey) => "report");// a record isn't read from the query string: it's inferred as the bodyapp.MapGet("/orders", (OrderQuery query) => query);app.Run();public record OrderQuery(string? Status, int Page = 1);

On .NET 10, the first version builds without a single warning and starts normally. Then the first request, to any URL, fails with a 500 and the "Body was inferred" exception, because endpoints are built when the first request arrives and the /orders handler can't be. Comment that endpoint out and, outside Development, /orders/5 answers an empty 400, and so does /reports with the header set. The second version answers Order 5, returns {"status":"open","page":2} for /orders?status=open&page=2, falls back to page 1 when page is missing, and still rejects ?page=two with a 400.

What you wrote vs. what binds

Handler parameterWhere the value comes fromWhat you see
int id with route {orderId}The query string, ?id=An empty 400 for every call
string apiKey meant as a headerThe query string, ?apiKey=A 400 for clients that send the header
OrderQuery query on a GETThe body, which GET doesn't allowA 500 on every endpoint
A service you forgot to registerThe body, or "UNKNOWN" next to a real bodyThe same 500 on every endpoint
[AsParameters] OrderQuery queryEach property, by the same rulesStatus and Page from the query

How to apply it in your project

  1. Match route names on purposeFor every {name} in a route template, check that a handler parameter has that exact name, or uses [FromRoute(Name = ...)]. Do it again whenever someone renames a route.
  2. Mark headers, forms and bodies on GETUse [FromHeader] for anything from a header, [FromForm] for form posts, and [FromBody] if a GET or DELETE really must read a body. None of these is ever inferred.
  3. Use AsParameters for filter objectsWhen a GET takes several query values, group them in a record and mark the parameter [AsParameters]. It binds each property by the normal rules; it isn't recursive model binding, so keep the record flat.
  4. Make one request in your testsAn integration test that sends any request through the app builds every endpoint, so a parameter the rules can't place fails the test instead of the first production call.

Key takeaways

  • Minimal APIs bind parameters with an ordered list of rules: attribute, special type, BindAsync, route-or-query for simple types, services, then the body.
  • A simple parameter is read from the route only if its name matches the template; otherwise it's read from the query string.
  • Headers and forms are never inferred, and complex types fall through to the body, which GET and DELETE don't allow implicitly.
  • A binding mistake shows up at runtime, as an empty 400 or a 500 on every endpoint, so attributes like [FromHeader] and [AsParameters] are behavior, not decoration.

📚 المصادر

  1. Parameter binding in Minimal API apps — Microsoft Learn
  2. Model binding in ASP.NET Core — Microsoft Learn
  3. Filters in Minimal API apps: parameter binding failures — Microsoft Learn

التعليقات (0)