Matching Action Method Parameters
Explore how to bind action method parameters in ASP.NET Core MVC from various sources including query strings, route data, headers, form fields, and request bodies. Understand the use of attributes to control binding, how complex types are mapped, and the conversion of parameter values. This lesson helps you master parameter matching and customization for building dynamic web applications.
We'll cover the following...
Action method parameters can be filled with information from various sources. In previous lessons, we have seen three of them: route parameters, query-string, and form fields.
By default, parameters that are simple types are taken from the route parameters and query-string, while complex types with several properties and collections are bound with data extracted from the request body. By default, no other sources are considered, but we can force the usage of specific sources with parameters’ attributes.
Value lookup and parameters’ attributes
We can force the source to be used for filling a parameter with an attribute that precedes a parameter.
The table below describes all options:
| Attribute | Example | Source |
|---|---|---|
| FromQueryAttribute | …, [FromQuery] string x, … | The value is taken from a query string parameter. |
| FromRouteAttribute | …, [FromRoute] string id, … | The value is taken from a route parameter. |
| FromHeaderAttribute | …, [FromHeader(Name=“Accept-Language”)] string id, … | The value is taken from a request header. |
| FromFormAttribute | …, [FromForm] MyModelType model, … | The value is taken from form parameters. |
| FromBodyAttribute | …, [FromBody] MyModelType model, … | The value is taken |