ASP.NET Core Web API Interview Question #76

What is routing in ASP.NET Core Web API?

Routing, Versioning, OpenAPI/Swagger & API Documentation Mid-Level Intermediate

Quick Interview Answer

Routing matches an incoming HTTP request to an API endpoint. ASP.NET Core considers route templates, HTTP methods, route values, constraints, and endpoint metadata when selecting the action that should handle a request.

Detailed Explanation

Routing matches an incoming HTTP request to an API endpoint. ASP.NET Core considers route templates, HTTP methods, route values, constraints, and endpoint metadata when selecting the action that should handle a request.

Clear routing is part of the public API contract. Routes should be stable, predictable, and centered on resources rather than internal implementation details.

Code Example

[ApiController]
[Route("api/products")]
public class ProductsController : ControllerBase
{
    [HttpGet("{id:int}")]
    public IActionResult Get(int id) => Ok(id);
}