Home > Blockchain >  ASP Core generate full paths for all endpoints
ASP Core generate full paths for all endpoints

Time:10-21

I need to retrieve all endpoints in an ASP Core application and generate their full paths.

example: /a/b/c/endpoint, x/y/endpoint,...

Those endpoints need to include controller actions as well as endpoints that are manually defined in StartUp using the Map*() methods.

I am able to retrieve a list of endpoints using:

services.GetRequiredService<EndpointDataSource>().Endpoints;

But I can't find a way how to create a corresponding full path for an Endpoint, especially if that endpoint is within a branched ASP pipeline. The Endpoint objects have a RoutePattern property that can be used but we still need the base path of the branched pipeline(s).

Cheers

CodePudding user response:

Paraphrasing your question, you have a branching request pipeline, calling .UseEndpoints in multiple branches. But while the .Map middleware adjusts Request.Path & Request.PathBase, end point routing is oblivious to this change.

I don't think this is supported in link generation. You should probably take a step back and re-evaluate if you really need to use .Map at all. Or at least the preserveMatchedPathSegment = true overload.

After a quick peek into the source code.

The .Map methods each create a separate IApplicationBuilder for that branched middleware pipeline.

Calling .UseRouting() creates an IEndpointRouteBuilder.

The IEndpointRouteBuilder is then used by .UseEndpoints() to define all the endpoints.

So here's a quick outline of what I'd attempt.

Write your own extension methods to replace .Map & .UseEndpoints and use those in your Startup class.

Store (or combine) the new path base in IApplicationBuilder.Properties.

Then in your .UseEndpoints() callback, you can call the real one. Then create your own datasource with EndpointDataSource src = new CompositeEndpointDataSource(IEndpointRouteBuilder.DataSources). Combine that with the path base you stored earlier.

And populate all that into some other service object for later use.

Unless someone can point out a simpler way?

  • Related