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
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.
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();
I've created the NuGet package "AspNetCore.RouteAnalyzer" that provides a feature to get all route information.
Try it if you'd like.
PM> Install-Package AspNetCore.RouteAnalyzer
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
....
});
}
Run project and you can access the url /routes to view all route information of your project.

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