Get all registered routes in ASP.NET Core

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 a route table in System.Web.Routing , is there something equivalent in ASP.NET Core? I want to get a list of routes in my Controller action.

+27
c # asp.net-core
source share
8 answers

I created the NuGet package "AspNetCore.RouteAnalyzer", which provides a function to get all the route information.

Try it if you want.

Using

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 .... }); } 

Overview

Run the project and you can access the URL /routes to see all the information about the route of your project.

+42
source share

You can take the ActionDescriptor collection from IActionDescriptorCollectionProvider . There you are looking for all the actions specified in the project, and you can take the AttributeRouteInfo or RouteValues , which contains all the information about the routes.

Example:

 public class EnvironmentController : Controller { private readonly IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider; public EnvironmentController(IActionDescriptorCollectionProvider actionDescriptorCollectionProvider) { _actionDescriptorCollectionProvider = actionDescriptorCollectionProvider; } [HttpGet("routes", Name = "ApiEnvironmentGetAllRoutes")] [Produces(typeof(ListResult<RouteModel>))] public IActionResult GetAllRoutes() { var result = new ListResult<RouteModel>(); var routes = _actionDescriptorCollectionProvider.ActionDescriptors.Items.Where( ad => ad.AttributeRouteInfo != null).Select(ad => new RouteModel { Name = ad.AttributeRouteInfo.Name, Template = ad.AttributeRouteInfo.Template }).ToList(); if (routes != null && routes.Any()) { result.Items = routes; result.Success = true; } return Ok(result); } } 
+16
source share

Using Swashbuckle helped me.

You just need to use AttributeRouting on the controllers you want to list (and their actions)

+5
source share

You can also use Template = x.AttributeRouteInfo.Template from the ActionDescriptors.Items array. Here is the full code 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); } } 
+3
source share

If you are not using MVC, call GetRouteData().Routers.OfType<RouteCollection>().First() to access the 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 }); 

Be sure to call GetRouteData() inside the route handler, otherwise it returns zero.

+2
source share

You are trying to get a router of type RouteCollection . To get all routes of this type, you must call .All() on routers of type RouteCollection .

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

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

Refer to the previous article if .All() does not work.

+1
source share

I also used IActionDescriptorCollectionProvider, getting information from RouteValues .

 var routes = _actionDescriptorCollectionProvider.ActionDescriptors.Items .Select(ad => new { Action = ad.RouteValues["action"], Controller = ad.RouteValues["controller"] }).Distinct().ToList(); 
+1
source share

Make a list of routes using ActionDescriptor Using the http method (get, publish, 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
source share

All Articles