The [FromHeader] attribute tells ASP.NET Core to bind a handler or page-handler parameter from an HTTP request header. It overrides the framework’s usual binding conventions, making the header source explicit. section 7.2 section 16.2.3
Binding a named header in a minimal API
Add Microsoft.AspNetCore.Mvc and decorate the parameter with [FromHeader]. The Name property maps the parameter to a differently named header: using Microsoft.AspNetCore.Mvc; app.MapGet("/products/{id}/paged", ([FromRoute] int id, [FromQuery] int page, [FromHeader(Name = "PageSize")] int pageSize) => $"Received id {id}, page {page}, pageSize {pageSize}"); A request containing the PageSize header supplies pageSize. Route and query values can often bind by convention, but header binding requires [FromHeader]. section 7.2
Listing 16.5 Choosing a binding source for model binding
public class PhotosModel: PageModel
{
public void OnPost(
[FromHeader] string userId, #1
[FromBody] List<Photo> photos) #2
{
/* method implementation */
}
}
Compare header binding in Razor Pages, where [FromHeader] can sit beside [FromBody] for a JSON request payload. section 16.2.3
In Razor Pages, applying [FromHeader] to a parameter such as userId binds the value from a header with that name. Use [FromBody] separately when JSON must be read from the request body. Validate bound values for formatting, expected values, and business rules. section 16.2.3 section 7.11
