Search code examples
c#asp.net-corerazor-pages

Asp.Net Core GetRoutes per assembly or namespace or project


I'm trying to get an overview of all accessible urls from my application. I have an application build up with with 2 projects (one is a default project, the other is a Razor Class Library).

In both projects are Razor pages defined and when I use AspNetCore.RouteAnalyzer (as seen in Get all registered routes in ASP.NET Core), I get an overview off all accessible routes. But I want to know the assembly, project or namespace of the routes. How can I achieve that?

July 28 2021 - Update with help of abdusco:

This information will be accessible from Asp.Net Core 6.0: https://github.com/dotnet/aspnetcore/issues/34759


Solution

  • You cannot retrieve the runtime type info for Razor pages before ASP.NET Core 6. You only have access to PageActionDescriptors, but sadly they don't provide type info.

    With ASP.NET Core 6, this is changed, and now you can get CompiledPageActionDescriptor for Razor pages, which does include the reflection info.

    var host = CreateHostBuilder(args).Build();
    var provider = host.Services.GetRequiredService<IActionDescriptorCollectionProvider>();
    var pageEndpoints = provider.ActionDescriptors.Items.OfType<CompiledPageActionDescriptor>()
        .Select(e => {
             var modelType = e.ModelTypeInfo;
             var route = e.DisplayName;
             // ... 
        });
    

    reflection