Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 777
Beaumind Avatar asked Jan 28 '17 10:01

Beaumind


People also ask

What is UseEndpoints in asp net core?

UseEndpoints adds endpoint execution to the middleware pipeline. It runs the delegate associated with the selected endpoint.

Where are routes registered in ASP NET MVC application?

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.

What is routing and how can you define routes in asp net core?

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.

How do you pass basic authentication in header .NET core?

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.


3 Answers

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.

like image 84
kobake Avatar answered Nov 07 '22 22:11

kobake


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

like image 31
Edgar Mesquita Avatar answered Nov 08 '22 00:11

Edgar Mesquita


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);
        }
      }
like image 21
kkost Avatar answered Nov 08 '22 00:11

kkost