5

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

John Shedletsky
  • 7,110
  • 12
  • 38
  • 63

1 Answers1

7

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.

edo.n
  • 2,184
  • 12
  • 12