4

I have an ASP.NET Core 5 Web API where routes are not matched. It works fine locally, but it's not working on the server. The logs show me

No candidates found for the request path

but I see the request path and it is correct.

I would like to log all the routes, just to see what my application thinks are the routes. It may help to see what is different on the server.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Don Chambers
  • 3,798
  • 9
  • 33
  • 74

2 Answers2

2

Try the following:

  1. Add IActionDescriptorCollectionProvider (from Microsoft.AspNetCore.Mvc.Infrastructure) as a parameter to Configure(...) within Startup.cs.
  2. Iterate over ActionDescriptors of IActionDescriptorCollectionProvider.

Example

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger log, IActionDescriptorCollectionProvider actionProvider)
{
    // configuration

    app.UseMvc();

    var routes = actionProvider.ActionDescriptors.Items
        .Where(x => x.AttributeRouteInfo != null);

    foreach(var route in routes)
    {
        log.LogDebug($"{route.AttributeRouteInfo.Template}");
    }
}
Jesse Johnson
  • 1,638
  • 15
  • 25
  • 2
    For ASP.NET Core 6, when you don't have a `Configure` method, you can get the action provider thus, before calling `app.Run()`: `var actionProvider = app.Services.GetRequiredService();` – sfuqua Jun 28 '22 at 19:34
  • Can you explain how `IActionDescriptorCollectionProvider` compares to `EndpointDataSource` ? – Dai Aug 12 '23 at 00:47
1

I would suggest using Swagger. It is a very common tool that lists all of the API controllers, endpoints and models. Setting it up for .Net 5 is well documented in Microsoft's sources: https://learn.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?view=aspnetcore-5.0#swagger-ui

Korfu
  • 673
  • 5
  • 20
  • 90% percent of questions here are from the people who decided to use Swagger. – Serge Dec 28 '21 at 23:09
  • 1
    So how does the swagger does this so that I can use this in my code? Eg for integration or funcional testing of my API. – Tomasz Juszczak Aug 01 '22 at 22:13
  • Swagger provides kind-of a frontend view that is useful to see all methods in the API. For manual testing it might be OK, but for testing an API I would go another way - simply creating a test project and testing the endpoints one-by-one. – Korfu Aug 02 '22 at 06:15