What is Model Binding in ASP.NET Core?
Question
Answers
Model binding in ASP.NET Core is the framework mechanism that automatically maps incoming HTTP request data to action parameters, model objects, and other .NET types.
Instead of manually reading values from the request, ASP.NET Core can construct the required object and populate its properties.
For example, a request might contain:
POST /api/orders Content-Type: application/json { "customerId": 1001, "quantity": 5 }
An ASP.NET Core action can receive a strongly typed model:
public IActionResult CreateOrder(OrderRequest request) { // request.CustomerId // request.Quantity }
The model binder handles the conversion from request data into the OrderRequest object.
Common binding sources
ASP.NET Core can bind data from sources such as:
- Route parameters
- Query-string parameters
- Form fields
- Request body
- Headers
- Uploaded files
Attributes can explicitly identify the source:
[FromRoute] [FromQuery] [FromBody] [FromHeader] [FromForm]
Model binding vs validation
An important distinction is:
Model binding answers:
"Can I construct the .NET object from the incoming request?"
Model validation answers:
"Is the resulting object valid according to the application's rules?"
For example:
public class CustomerRequest { [Required] public string Name { get; set; } [Range(18, 100)] public int Age { get; set; } }
Model binding populates Name and Age, while validation evaluates the attributes.
Expert consideration
In APIs, developers should carefully control what properties can be bound, particularly when dealing with update operations. Binding an overly broad domain/entity model directly from client input can create over-posting or unintended property modification risks.
A better architecture is often to use request DTOs specifically designed for the API contract.