0

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

CribAd
  • 452
  • 6
  • 19
  • [Here](https://github.com/kobake/AspNetCore.RouteAnalyzer/blob/master/AspNetCore.RouteAnalyzer/Inner/RouteAnalyzerImpl.cs#L25) you can see that an `ActionDescriptor` will be provided by ASP.NET Core. Via `_e.ControllerDescriptor.ControllerType.Assembly` you should get the assembly. But you either have to copy the code and implement it on your own or ask the maintainer for a change – mu88 Jul 26 '21 at 13:45
  • Tnx mu88, I copied the code for customization. But the I get the error `_e.ControllerDescriptor.ControllerType.Assembly: error CS1061: 'ActionDescriptor' does not contain a definition for 'ControllerDescriptor' and no accessible extension method 'ControllerDescriptor' accepting a first argument of type 'ActionDescriptor' could be found (are you missing a using directive or an assembly reference?)`. What am I missing? – CribAd Jul 26 '21 at 14:25
  • Ah yes, I see: `ControllerDescriptor` does not exist in ASP.NET Core anymore. Can you take a look into `_e.BoundProperties` and `_e.Properties` whether they contain something useful? – mu88 Jul 26 '21 at 14:41
  • See: https://stackoverflow.com/questions/28435734/how-to-get-a-list-of-all-routes-in-asp-net-core/66086633#66086633 and filter endpoints by controller / razor page assembly – abdusco Jul 26 '21 at 16:03
  • From the [AspNetCore.RouteAnalyzer document](https://github.com/kobake/AspNetCore.RouteAnalyzer), I didn't find the related description about get the assembly, project, or namespace of the routes. As a workaround, I suggest you could [use Areas](https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/areas?view=aspnetcore-5.0), you could store the assembly, project, or namespace name via the Areas, then, when you get the route information, you could base on the areas to detect where the route is from. – Zhi Lv Jul 27 '21 at 03:15
  • @mu88 unfortunately the `BoundProperties` are `null` and the `Properties` where everything is null or empty. Nowhere in _e I can find any assembly or namespace data. – CribAd Jul 27 '21 at 06:44
  • @abdusco trying to use that method, but also in the `RouteEndpoint` is no namespace or assembly data available, where do you think I need to have a look> – CribAd Jul 27 '21 at 06:45
  • @ZhiLv I'm aware of that workaround, but then I need to reconstruct my whole project and I'm trying to prevent that – CribAd Jul 27 '21 at 06:46
  • @CribAd Yeah, noticed that as well. `xModel` class of a Razor page is deep within the `Target` property (compiledpageaction-something). The only way to reach it is with reflection, or some other provider that actually returns `CompiledPageActionDescriptor`, which I haven't yet deciphered. https://github.com/dotnet/aspnetcore/blob/2be49d930a5fb53e781abd175c3b2a8f8b7827d4/src/Mvc/Mvc.RazorPages/src/CompiledPageActionDescriptor.cs#L46-L51 – abdusco Jul 27 '21 at 06:58
  • Maybe it would be easier to retrieve the information manually via reflection and match it via controller name derived from `AspNetCore.RouteAnalyzer` – mu88 Jul 27 '21 at 09:28
  • @mu88 Getting controller info is easy, the problem is with Razor pages. Metadata provided by ASP.NET Core is too high-level to be of any use. It doesn't provide page class types for page actions – abdusco Jul 27 '21 at 09:43
  • Maybe you better start from the other end: dig into the ASP.NET Core code and try to find out how the runtime itself determines which Razor pages to host and how it does this. – mu88 Jul 27 '21 at 09:48
  • Indeed, the info given in all models I can get from EndPoints do'nt have Controller information for Razor Pages. I will try to find out how Asp.Net Core itself works. Never dived into it, so it will be a exciting ride... – CribAd Jul 27 '21 at 10:06
  • Tried a lot, but could not get the correct Assembly from some Route object nor Route-information from a specific Assambly. I think I need to give up and create a workaround. – CribAd Jul 27 '21 at 15:38
  • 1
    @CribAd I've asked ASP.NET Core maintainers, and that seems to be only possible in ASP.NET Core 6: https://github.com/dotnet/aspnetcore/issues/34759 – abdusco Jul 27 '21 at 21:58
  • Thanks @abdusco, nice work. I'll use my custom workaround until I'm able to upgrade to 6.0. – CribAd Jul 28 '21 at 07:12
  • @CribAd I'll add the answer for ASP.NET Core 6, it will be useful after it's released in November 2021. – abdusco Jul 28 '21 at 07:14

1 Answers1

1

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

abdusco
  • 9,700
  • 2
  • 27
  • 44