Get all registered routes in ASP.NET Core

前端 未结 8 1375
独厮守ぢ
独厮守ぢ 2020-12-02 12:07

I am new to .NET Core. I want to get a list of all registered routes in ASP.NET Core. In ASP.NET MVC we had route table in System.Web.Routing, is there somethin

相关标签:
8条回答
  • 2020-12-02 12:43

    You are trying to get a router of type RouteCollection. To get all routes of that type you should be able to call .All() on routers of type RouteCollection.

    Example: var routes = RouteData.Routers.OfType<RouteCollection>().All();

    Credit to: https://rimdev.io/get-registered-routes-from-an-asp.net-mvc-core-application/

    Refer to above article if .All() doesn't work.

    0 讨论(0)
  • 2020-12-02 12:43

    I did also using IActionDescriptorCollectionProvider getting the information from the RouteValues.

    var routes = _actionDescriptorCollectionProvider.ActionDescriptors.Items
        .Select(ad => new
        {
            Action = ad.RouteValues["action"],
            Controller = ad.RouteValues["controller"]
        }).Distinct().ToList();
    
    0 讨论(0)
  • 2020-12-02 12:45

    I've created the NuGet package "AspNetCore.RouteAnalyzer" that provides a feature to get all route information.

    • NuGet Gallery | AspNetCore.RouteAnalyzer ... Package on NuGet Gallery
    • kobake/AspNetCore.RouteAnalyzer ... Usage guide

    Try it if you'd like.

    Usage

    Package Manager Console

    PM> Install-Package AspNetCore.RouteAnalyzer
    

    Startup.cs

    using AspNetCore.RouteAnalyzer; // Add
    .....
    public void ConfigureServices(IServiceCollection services)
    {
        ....
        services.AddRouteAnalyzer(); // Add
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        ....
        app.UseMvc(routes =>
        {
            routes.MapRouteAnalyzer("/routes"); // Add
            ....
        });
    }
    

    Browse

    Run project and you can access the url /routes to view all route information of your project.

    0 讨论(0)
  • 2020-12-02 12:48

    You can also use Template = x.AttributeRouteInfo.Template value from ActionDescriptors.Items array. Here is a full code sample from there :

        [Route("monitor")]
        public class MonitorController : Controller {
            private readonly IActionDescriptorCollectionProvider _provider;
    
            public MonitorController(IActionDescriptorCollectionProvider provider) {
              _provider = provider;
            }
    
            [HttpGet("routes")]
            public IActionResult GetRoutes() {
                var routes = _provider.ActionDescriptors.Items.Select(x => new { 
                   Action = x.RouteValues["Action"], 
                   Controller = x.RouteValues["Controller"], 
                   Name = x.AttributeRouteInfo.Name, 
                   Template = x.AttributeRouteInfo.Template 
                }).ToList();
                return Ok(routes);
            }
          }
    
    0 讨论(0)
  • 2020-12-02 12:59

    If you don't use MVC call GetRouteData().Routers.OfType<RouteCollection>().First() for access to RouteCollection:

    app.UseRouter(r => {
        r.MapGet("getroutes", async context => {
            var routes = context.GetRouteData().Routers.OfType<RouteCollection>().First();
            await context.Response.WriteAsync("Total number of routes: " + routes.Count.ToString() + Environment.NewLine);
            for (int i = 0; i < routes.Count; i++)
            {
                await context.Response.WriteAsync(routes[i].ToString() + Environment.NewLine);
            }               
        });
        // ...
        // other routes
    });
    

    Make sure to call GetRouteData() inside route handler otherwise it returns null.

    0 讨论(0)
  • 2020-12-02 13:00

    List routes using ActionDescriptor WITH http method (get,post etc)

    [Route("")]
    [ApiController]
    public class RootController : ControllerBase
    {
        private readonly IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider;
    
        public RootController(IActionDescriptorCollectionProvider actionDescriptorCollectionProvider)
        {
            _actionDescriptorCollectionProvider = actionDescriptorCollectionProvider;
        }
    
        public RootResultModel Get()
        {
            var routes = _actionDescriptorCollectionProvider.ActionDescriptors.Items.Where(
                ad => ad.AttributeRouteInfo != null).Select(ad => new RouteModel
            {
                Name = ad.AttributeRouteInfo.Template,
                Method = ad.ActionConstraints?.OfType<HttpMethodActionConstraint>().FirstOrDefault()?.HttpMethods.First(),
                }).ToList();
    
            var res = new RootResultModel
            {
                Routes = routes
            };
    
            return res;
        }
    }
    

    Result

    0 讨论(0)
提交回复
热议问题