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!

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: