Routing In Asp.net core

mina shaker 0 Reputation points
2024-08-23T06:45:32.9066667+00:00

i was reading about routing in asp.net core but i could not understand where predifined routes are stored at compile time

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,526 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Gowtham CP 4,820 Reputation points
    2024-08-23T09:42:11.3933333+00:00

    Hello mina shaker ,

    Thanks for reaching out on Microsoft Q and A.

    In ASP.NET Core, routing is how the app decides which code to run based on the URL. When a request comes in, it matches the URL against the route templates you've defined, usually in the Startup.cs or Program.cs file. If it finds a match, it directs the request to the correct controller action to handle it.

    For example, routes are defined like this:

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

    These routes are set up in the code and stored in memory, ready to handle requests as soon as your app starts.

    Hope this helps! If you have any more questions, just let me know!

    If this information helps, please upvote and accept this answer to close the thread. Thanks!


  2. Bruce (SqlWork.com) 64,481 Reputation points
    2024-08-23T17:31:05.9266667+00:00

    a key feature of asp.net core was middleware support. Early on, a common request was that middleware could know what the route and route delegate were. For example, was this a MVC request, static file, etc. So routing was broken into two middleware. Routing which fills in route data and picks the endpoint delegate. this can run as early middleware. And endpoint middleware which actually calls the delegate. this is run as late middleware.

    the routing tables are stored as an injected data, and built at startup. there are several sources of route table data (IEnumerable<EndpointDataSource>), endpoint mapping, routing attributes and razor page names. (reflection is used build route table for attributes and razor page names at startup).

    you can add a route table dumper like this:

    app.MapGet("/debug/routes", (IEnumerable<EndpointDataSource> endpointSources) =>
    {
        var sb = new StringBuilder();
        var endpoints = endpointSources.SelectMany(es => es.Endpoints);
        foreach (var endpoint in endpoints)
        {
            if(endpoint is RouteEndpoint routeEndpoint)
            {
                _ = routeEndpoint.RoutePattern.RawText;
                _ = routeEndpoint.RoutePattern.PathSegments;
                _ = routeEndpoint.RoutePattern.Parameters;
                _ = routeEndpoint.RoutePattern.InboundPrecedence;
                _ = routeEndpoint.RoutePattern.OutboundPrecedence;
            }
    
            var routeNameMetadata = endpoint.Metadata.OfType<Microsoft.AspNetCore.Routing.RouteNameMetadata>().FirstOrDefault();
            _ = routeNameMetadata?.RouteName;
    
            var httpMethodsMetadata = endpoint.Metadata.OfType<HttpMethodMetadata>().FirstOrDefault();
            _ = httpMethodsMetadata?.HttpMethods; // [GET, POST, ...]
    
            // There are many more metadata types available...
    });
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.