ASP.NET Core Web API Interview Question #36
What happens when ModelState is invalid in a controller marked with [ApiController]?
DTOs, Model Binding, Validation & API Responses Junior Beginner
Quick Interview Answer
ApiController automatically returns an HTTP 400 response for model-validation failures instead of requiring every action to manually check ModelState.IsValid.
Detailed Explanation
ApiController automatically returns an HTTP 400 response for model-validation failures instead of requiring every action to manually check ModelState.IsValid.
The default error response follows ASP.NET Core's validation problem-details behavior. Applications can customize invalid-model-state responses when they need a consistent organization-specific error contract.
Code Example
[ApiController]
[Route("api/customers")]
public class CustomersController : ControllerBase
{
[HttpPost]
public IActionResult Create(CreateCustomerRequest request)
{
// Normally reached only when model validation succeeds.
return Ok();
}
}