- Home
- .NET coding challenges
- Add model binding in an ASP.NET Core Web API
Add model binding in an ASP.NET Core Web API
Model binding in an ASP.NET Core Web API or MVC app allows us to bind a model based on a source.
This can be through the query string, a form value, or a header value.
In an API controller, we can use the [FromHeader]
attribute to bind a parameter to a HTTP request header value in an action.
In the example below, how can we add the [FromHeader]
attribute so the authorization
parameter binds to the Authorization
header in the HTTP request?
[ApiController, Route("api/my-data")]
public class MyDataController : Controller
{
[HttpGet]
public string? MyData(string? authorization)
{
return authorization;
}
}
To help you out, you can learn more about ASP.NET Core model binding by watching our video:
Give us your anonymous feedback regarding this page, or any other areas of the website.