Back to Aspnetcore

Parameter Binding Summary8 10

aspnetcore/fundamentals/minimal-apis/includes/parameter-binding-summary8-10.md

latest1.8 KB
Original Source

:::moniker range=">= aspnetcore-8.0"

Parameter binding is the process of converting request data into strongly typed parameters that are expressed by route handlers. A binding source determines where parameters are bound from. Binding sources can be explicit or inferred based on HTTP method and parameter type.

Supported binding sources:

  • Route values
  • Query string
  • Header
  • Body (as JSON)
  • Form values
  • Services provided by dependency injection
  • Custom

The following GET route handler uses some of these parameter binding sources:

:::code language="csharp" source="~/fundamentals/minimal-apis/7.0-samples/WebMinAPIs/Program.cs" id="snippet_pbg" highlight="8-11":::

Key parameter binding features

  • Explicit binding: Use attributes like [FromRoute], [FromQuery], [FromHeader], [FromBody], [FromForm], and [FromServices] to explicitly specify binding sources.
  • Form binding: Bind form values using [FromForm] attribute, including support for IFormFile and IFormFileCollection for file uploads.
  • Complex types: Bind to collections and complex types from forms, query strings, and headers.
  • Custom binding: Implement custom binding logic using TryParse, BindAsync, or the IBindableFromHttpContext<T> interface.
  • Optional parameters: Support nullable types and default values for optional parameters.
  • Dependency injection: Parameters are automatically bound from services registered in the DI container.
  • Special types: Automatic binding for HttpContext, HttpRequest, HttpResponse, CancellationToken, ClaimsPrincipal, Stream, and PipeReader.

Learn more: For detailed information on parameter binding including advanced scenarios, validation, binding precedence, and troubleshooting, see xref:fundamentals/minimal-apis/parameter-binding.

:::moniker-end