ASP.NET Core Interview Question #116
How does ASP.NET Core bind complex types?
Routing, Controllers & Model Binding Mid-Level Intermediate
Quick Interview Answer
For complex types, model binding creates an object and binds matching request values to its public settable properties or supported constructor parameters.
Detailed Explanation
For complex types, model binding creates an object and binds matching request values to its public settable properties or supported constructor parameters.
In traditional MVC form posts, property names in form fields are matched to model property names. For API request bodies, input formatters such as the JSON formatter deserialize the body into the target type.
Dedicated input models or DTOs are preferable to binding database entities directly because they make the accepted input contract explicit and reduce overposting risk.
Code Example
public class CreateProductRequest
{
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
}
[HttpPost]
public IActionResult Create(CreateProductRequest request)
{
return Ok(request);
}