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!

Be First to Comment

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.