Routing in ASP.NET Core (UseRouting, Endpoints)
๐ก Concept Name
Routing in ASP.NET Core (UseRouting, UseEndpoints)
๐ Quick Intro
Routing in ASP.NET Core is a mechanism for directing incoming HTTP requests to the appropriate controller actions or endpoints. It is configured using `UseRouting()` and `UseEndpoints()` middleware. It supports both conventional and attribute routing.
๐ง Analogy / Short Story
Imagine a city with roads leading to different houses. Routing is like the city map: when a vehicle (HTTP request) enters the city, the map (router) determines which house (controller/action) it should go to based on the address (URL).
๐ง Technical Explanation
Routing in ASP.NET Core is middleware-based. First, `UseRouting()` enables route matching, parsing the URL path. Then `UseEndpoints()` executes the matched endpoint logic. You define routes using either convention-based patterns or attributes directly on controllers/actions. Endpoint routing unifies MVC, Razor Pages, and minimal APIs under a common routing system.
๐ฏ Purpose & Use Case
- โ Map HTTP requests to controller actions or Razor pages
- โ Enable clean, SEO-friendly URLs
- โ Organize routes using attributes or conventions
- โ Support middleware branching and pipelines
- โ Integrate with authorization and logging pipelines
๐ป Real Code Example
Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
var app = builder.Build();
// Enable routing
app.UseRouting();
// Optional: Authentication/Authorization middleware here
// Map endpoints
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});

โ Interview Q&A
Q1: What is routing in ASP.NET Core?
A: It's a system to match incoming requests to controller actions or endpoints.
Q2: What does UseRouting() do?
A: It parses the URL and matches it to a route.
Q3: What is UseEndpoints() for?
A: Executes the matched endpoint logic after routing is complete.
Q4: Where should UseRouting() appear in middleware pipeline?
A: Before UseEndpoints().
Q5: Can we use attribute routing?
A: Yes, by using [Route] and [HttpGet] etc. on controller actions.
Q6: What is endpoint routing?
A: Unified way to handle routing across MVC, Razor Pages, and minimal APIs.
Q7: Can you define multiple routes for a single action?
A: Yes, by using multiple [Route] attributes.
Q8: How do you route to Razor Pages?
A: Using endpoints.MapRazorPages().
Q9: Can middleware access route data?
A: Yes, via HttpContext.GetRouteData().
Q10: What happens if no route matches?
A: A 404 Not Found response is returned.
Q1: Which middleware is used to enable routing in ASP.NET Core?
- A. UseRouting()
- B. UseAuthorization()
- C. UseEndpoints()
- D. UseStaticFiles()
Q2: What happens if UseEndpoints() is not called?
- A. Route matching will work but no endpoint will be executed.
- B. Controllers will still execute.
- C. It will use attribute routing instead.
- D. Static files will be served.
๐ก Bonus Insight
You can also use middleware-based routing for custom branches in the pipeline using Map()
, MapWhen()
, and MapGet()
for minimal APIs. This adds flexibility for microservices and modular apps.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!