ASP.NET Core Web API Interview Question #27
What do [FromRoute], [FromQuery], [FromBody], [FromHeader], and [FromForm] mean?
DTOs, Model Binding, Validation & API Responses Junior Beginner
Quick Interview Answer
These attributes explicitly tell ASP.NET Core where an action parameter should be obtained. FromRoute reads route values, FromQuery reads the query string, FromBody deserializes the request body, FromHeader reads an HTTP header, and FromForm reads form data.
Detailed Explanation
These attributes explicitly tell ASP.NET Core where an action parameter should be obtained. FromRoute reads route values, FromQuery reads the query string, FromBody deserializes the request body, FromHeader reads an HTTP header, and FromForm reads form data.
Explicit binding sources can make an API contract easier to understand, especially when an endpoint combines several input sources.
Code Example
[HttpPost("{id:int}")]
public IActionResult Update(
[FromRoute] int id,
[FromBody] UpdateProductRequest request,
[FromHeader(Name = "X-Correlation-Id")] string? correlationId)
{
return NoContent();
}