Skip to content

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.

Routing is not just a URL concern. It shapes the public contract of the service, which means it affects discoverability, versioning, and maintainability.

  • keep route structures resource-oriented
  • use HTTP verbs consistently
  • return explicit status codes
  • keep controllers thin and move business logic into services
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpGet("{id:int}")]
public IActionResult GetById(int id) => Ok(new { id });
}