ASP.NET Core Interview Question #85

What is UseRouting() in ASP.NET Core?

Middleware & Request Pipeline Mid-Level Intermediate

Quick Interview Answer

UseRouting adds endpoint routing middleware that matches the incoming request to an endpoint based on the application's route definitions and metadata.

Detailed Explanation

UseRouting adds endpoint routing middleware that matches the incoming request to an endpoint based on the application's route definitions and metadata.

In middleware-oriented configurations, routing determines which endpoint is selected before middleware such as authorization evaluates endpoint metadata. Endpoint execution then occurs through the configured endpoint mappings.

Modern ASP.NET Core templates can make some routing behavior appear implicit, especially with minimal APIs, but understanding endpoint routing remains important when ordering middleware and applying policies.

Code Example

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");