ASP.NET Core Interview Question #119
What is the ApiController attribute?
Routing, Controllers & Model Binding Mid-Level Intermediate
Quick Interview Answer
ApiController enables API-focused controller conventions and behaviors. It is normally applied to API controllers, often together with attribute routing.
Detailed Explanation
ApiController enables API-focused controller conventions and behaviors. It is normally applied to API controllers, often together with attribute routing.
Important behavior includes automatic HTTP 400 responses for invalid model state, binding-source inference for action parameters, and API-specific error response behavior.
Because automatic validation responses can execute before the action method, developers should understand this behavior when debugging or customizing API validation.
Code Example
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpPost]
public ActionResult<ProductDto> Create(CreateProductRequest request)
{
return Ok();
}
}