Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all registered routes in ASP.NET Core 3.0

Tags:

asp.net-core

I'm trying to debug an issue with routes where I'm not sure what url an external app has to hit to trigger my controllers correctly. I just want to print them all out.

I found Get all registered routes in ASP.NET Core but none of the answers seem to work in Asp.net Core 3.0

I also found https://www.nuget.org/packages/AspNetCore.RouteAnalyzer, but it's for Core 2.0

like image 854
John Shedletsky Avatar asked Jun 22 '20 05:06

John Shedletsky


1 Answers

I just want to print them all out.

So, I guess this is for debugging purposes?

In that case, this will get you started:

public HomeController(IActionDescriptorCollectionProvider provider)
{
    this.provider = provider;
}

public IActionResult Index()
{
    var urls = this.provider.ActionDescriptors.Items
        .Select(descriptor => '/' + string.Join('/', descriptor.RouteValues.Values
                                                                        .Where(v => v != null)
                                                                        .Select(c => c.ToLower())
                                                                        .Reverse()))
        .Distinct()
        .ToList();

    return Ok(urls);
}

This will return response in the following format:

[
    "/post/delete",
    "/users/index/administration",
    "/users/details/administration",
]

If you need more information, the IActionDescriptorCollectionProvider has plenty of it.

like image 132
edo.n Avatar answered Nov 15 '22 08:11

edo.n