Get all registered routes in ASP.NET Core 3.0

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

Related