Controllers and Routing in ASP.NET Core Web API
Controllers and Routing in ASP.NET Core Web API
Section titled βControllers and Routing in ASP.NET Core Web APIβControllers expose the HTTP surface of an API. Routing determines which action handles an incoming request.
Why This Matters
Section titled βWhy This MattersβRouting is not just a URL concern. It shapes the public contract of the service, which means it affects discoverability, versioning, and maintainability.
Core Design Guidance
Section titled βCore Design Guidanceβ- keep route structures resource-oriented
- use HTTP verbs consistently
- return explicit status codes
- keep controllers thin and move business logic into services
Minimal Example
Section titled βMinimal Exampleβ[ApiController][Route("api/[controller]")]public class ProductsController : ControllerBase{ [HttpGet("{id:int}")] public IActionResult GetById(int id) => Ok(new { id });}