AspNetCore.RouteAnalyzer icon indicating copy to clipboard operation
AspNetCore.RouteAnalyzer copied to clipboard

Snippet to use with .Net Core 3.x using Endpoint routing

Open guibirow opened this issue 4 years ago • 1 comments

I ended here looking for same thing as many, given .Net Core 3+ now uses Endpoint routing, this library is not needed anymore.

To save you some time, the following code will give you similar results:


        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapGet("/routes", request =>
                {
                    request.Response.Headers.Add("content-type", "application/json");

                    var ep = endpoints.DataSources.First().Endpoints.Select(e => e as RouteEndpoint);
                    return request.Response.WriteAsync(
                        JsonSerializer.Serialize(
                            ep.Select(e => new
                            {
                                Method = ((HttpMethodMetadata)e.Metadata.First(m => m.GetType() == typeof(HttpMethodMetadata))).HttpMethods.First(),
                                Route = e.RoutePattern.RawText
                            })
                        )
                    );
                });
            });
        }

Just tweak with data you need from the routes.

guibirow avatar Jun 03 '20 15:06 guibirow

@guibirow Thanks!

richarddavenport avatar Jun 25 '20 21:06 richarddavenport