How to see all the registered routes in an ASP.NET Core app

How to see all the registered routes in an ASP.NET Core app

I've been trying to debug a 404 on a minimal API endpoint, and wanted to check what routes were registered.

Adding this to Program.cs registered an endpoint /debug-routes that dumped them all out...

app.Use(async (context, next) => {
  Console.WriteLine(context.Request.Path);
  if (context.Request.Path == "/debug-routes") {
    EndpointDataSource? endpointDataSource = context.RequestServices.GetService<EndpointDataSource>();
    List<string?>? routes = endpointDataSource?.Endpoints.OfType<RouteEndpoint>()
      .Where(e => e.RoutePattern.RawText is not null 
                  && !e.RoutePattern.RawText.StartsWith("_framework") 
                  && !e.RoutePattern.RawText.StartsWith("_content")
                  && !e.RoutePattern.RawText.StartsWith("/_blazor"))
      .Select(e => e.RoutePattern.RawText)
      .ToList();
    await context.Response.WriteAsync(string.Join("\n", routes ?? []));
    return;
  }
  await next();
});

Hmm, that was an unusually waffle-free post for me 😆. Have to try harder next time!

Note: As of version 2.6.0, this is included in the Pixata.AspNetCore Nuget package. See the Route dumping section of the Readme.md file there for details on how to use it.

Comments

No approved comments yet.

Leave a comment