What features do controllers provide out of the box that minimal APIs don't have native equivalents for?
Short answer
Controllers provide built-in support for action filters/exception filters/result filters (a rich, multi-stage filter pipeline), automatic model validation via [ApiController]'s ModelState checking, convention-based routing, and view rendering (for MVC views/Razor Pages) --…
Advertisement
AModel answer
Controllers provide built-in support for action filters/exception filters/result filters (a rich, multi-stage filter pipeline), automatic model validation via [ApiController]'s ModelState checking, convention-based routing, and view rendering (for MVC views/Razor Pages) -- minimal APIs instead use the lighter-weight IEndpointFilter for cross-cutting concerns and require more explicit, manual validation logic, reflecting a deliberate design trade-off favoring simplicity and lower overhead over built-in richness.
// Controllers: automatic model validation via [ApiController]
[ApiController]
public class ProductsController : ControllerBase {
[HttpPost]
public IActionResult Create(ProductDto dto) {
// ModelState.IsValid already checked automatically, 400 returned if invalid
return Ok(_service.Create(dto));
}
}
// Minimal API: validation typically requires explicit logic or a library like FluentValidationReal-world example
A team migrating a validation-heavy controller-based API to minimal APIs discovers they need to add explicit validation middleware or a library like MinimalApis.Extensions to replicate the automatic ModelState validation they previously got for free with [ApiController].
Likely follow-ups
- How does IEndpointFilter provide similar functionality to MVC's filter pipeline?
- What third-party libraries fill the validation gap for minimal APIs?
Related topics