ASP.NET Core Web API Interview Question #29
Can an API action have more than one parameter bound from the request body?
DTOs, Model Binding, Validation & API Responses Junior Beginner
Quick Interview Answer
The request body is a single stream and is normally deserialized once, so an API action should not define multiple independent FromBody parameters. Instead, create one request DTO containing all body values required by the operation.
Detailed Explanation
The request body is a single stream and is normally deserialized once, so an API action should not define multiple independent FromBody parameters. Instead, create one request DTO containing all body values required by the operation.
Route, query, and header values can still be separate action parameters alongside the single body DTO.
Code Example
public sealed class CreateOrderRequest
{
public int CustomerId { get; init; }
public List<OrderLineRequest> Lines { get; init; } = [];
}
[HttpPost]
public IActionResult Create(
[FromBody] CreateOrderRequest request) => Ok();