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 something equivalent in ASP.NET Core? I want to get the routes list in my Controller Action.
UseEndpoints adds endpoint execution to the middleware pipeline. It runs the delegate associated with the selected endpoint.
Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder.
In ASP.NET, routing is the process of directing the HTTP requests to the right controller. The MVC middleware must decide whether a request should go to the controller for processing or not. The middleware makes this decision based on the URL and some configuration information.
Basic authentication works as follows: If a request requires authentication, the server returns 401 (Unauthorized). The response includes a WWW-Authenticate header, indicating the server supports Basic authentication. The client sends another request, with the client credentials in the Authorization header.
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 take an ActionDescriptor
collection from IActionDescriptorCollectionProvider
.
In there, you can see all actions referred to in the project and can take an AttributeRouteInfo
or RouteValues
, which contain all information about the routes.
Example:
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
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);
}
}
internal class RouteModel
{
public string Name { get; set; }
public string Template { get; set; }
}
internal class ListResult<T>
{
public ListResult()
{
}
public List<RouteModel> Items { get; internal set; }
public bool Success { get; internal set; }
}
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With