How does the "UseRouting" method work in ASP.NET Core?

Hikljh Dima 20 Reputation points
2023-08-21T23:21:18.9733333+00:00

I've read that UseRouting matches a request to an endpoint, and UseEndpoints executes the matched endpoint. However, we have middleware in this order:

// ...Another Middleware

UseRouting(); 

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


app.Map("/something", (context) => context.Response.WriteAsync("Hello World!"));

// ...Another Middleware

So, how can the UseRouting method match a request (like https://localhost:5000/Home/Index) to endpoints (like /Home/Index) if it invoke before MapControllerRoute? In other words, how does the UseRouting method know about this endpoint (/Home/Index) to make the comparison?"

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,459 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,238 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,312 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,380 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 57,886 Reputation points
    2023-08-22T22:07:32.0333333+00:00

    in addition to routing middleware, there is also routing metadata.

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

    the .Map... methods are not registering middleware. the method appends routing data to the endpoint metadata. by the time app.Run() is called and any middleware is actually called, the metadata has been completely specified.

    the UseRouting() is the middleware that matches the url to an endpoint.

    the UseEndpoints() is the middleware that actually executes the matched endpoint.

    so any middleware after the UseRouting() and before UseEndpoints() can access the route data ( the controller name, action, etc) and will run before the endpoint.

    note: if not specified UseEndpoints() is appended to the middleware

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jerry Fu - MSFT 561 Reputation points Microsoft Vendor
    2023-08-22T06:33:15.4+00:00

    There is one thing to mention. If you don't use "app.useRouting()" . Asp.net core will automatically use it at the beginning of middleware pipeline. But when you manually use it, you are forcing a pipeline order to use it.

    When app.useRouting execute, it will find all the possible endpoints throughout the project. Endpoint could be defined by app.MapControllerRoute() or app.MapControllers or app.Map etc. It doesn't matter this endpoint is defined before "useRouting" or after. "useRouting" find them like regarding the whole project code as text.

    You could add the below code to anywhere of your pipeline.

    // Location 1: before routing runs, endpoint is always null here
    app.Use((context, next) =>
    {
        Console.WriteLine($"1. Endpoint: {context.GetEndpoint()?.DisplayName ?? "(null)"}");
        return next(context);
    });
    
    app.UseRouting();
    
    // Location 2: after routing runs, endpoint will be non-null if routing found a match
    app.Use((context, next) =>
    {
        Console.WriteLine($"2. Endpoint: {context.GetEndpoint()?.DisplayName ?? "(null)"}");
        return next(context);
    });
    
    

    When you visit any matching URL. You will find it always find the endpoint after you use "useRouting"User's image

    Also, as what I mentioned in the beginning, deleting "app.useRouting()" also works to find endpoint. And for most of the scenario. It makes no difference.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.