Routing
The idea
Routing is the process of matching an incoming HTTP request URL to a specific handler in the application. A handler can be an MVC controller action, a Razor Page, or a Minimal API endpoint.
Configuration
Routing is configured in Program.cs. You call app.UseRouting() to add the route-matching middleware, then app.UseEndpoints(endpoints => { ... }) to define the actual routes and map them to handlers. Both are in the middleware listThe middleware pipelineHow a request passes through a chain of components, and why their order matters.Opens in a new tab. In .NET 6 and later templates, WebApplication adds the routing middleware for you, so you may not see these two calls in a new project.
app.UseRouting();
app.UseEndpoints(endpoints => { ... });Types of routing
- Convention-based routing defines global routing patterns, and is often used in MVC applications. For example,
"{controller=Home}/{action=Index}/{id?}". - Attribute routing defines routes directly on controllers and action methods, using attributes like
[Route(...)]and[HttpGet(...)]. It’s the preferred method for Web APIs because it’s explicit.
When the routes are found
With attribute routing, ASP.NET Core reflects over your controllers once, at startup. It scans the [Route], [HttpGet] and [HttpPost] attributesAttributes and reflectionAttributes tag code with metadata, and reflection reads that metadata back at runtime.Opens in a new tab, builds a route table, and caches it. Every request then uses the cached table. It doesn’t reflect again on each HTTP call, which would be far too slow.
Sources
- ASP.NET Core fundamentals overview, Microsoft Learn · accessed May 16, 2025