Forked from mckabue/Get_All_Routes_in_ASP.NET_MVC_Core_2.cs
Last active
September 6, 2018 07:12
-
-
Save rmarskell/b2f24fbd0e731ef157e375ccf09cef75 to your computer and use it in GitHub Desktop.
Get all routes, including default routes specified at start up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Authorization; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Mvc.Abstractions; | |
using Microsoft.AspNetCore.Mvc.Controllers; | |
using Microsoft.AspNetCore.Mvc.Infrastructure; | |
using Microsoft.AspNetCore.Mvc.Internal; | |
using Microsoft.AspNetCore.Routing; | |
using Microsoft.Extensions.DependencyInjection; | |
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace Extensions { | |
public static class GetAllRoutes { | |
public static IApplicationBuilder AllRoutes( this IRouteBuilder routeBuilder, PathString pathString ) { | |
var app = routeBuilder.ApplicationBuilder; | |
app.Map( pathString, builder => { | |
builder.UseMiddleware<GetRoutes>( routeBuilder ); | |
} ); | |
return app; | |
} | |
public static List<T> GetCustomAttributes<T>( this ActionDescriptor actionDescriptor ) where T : Attribute { | |
var controllerActionDescriptor = actionDescriptor as ControllerActionDescriptor; | |
if ( controllerActionDescriptor != null ) { | |
return controllerActionDescriptor.MethodInfo.ReflectedType.GetCustomAttributes( typeof( T ), true ).Select( a => (T)a ).ToList(); | |
} | |
return new List<T>(); | |
} | |
} | |
public class GetRoutes { | |
private IRouteBuilder _routeBuilder; | |
public GetRoutes( RequestDelegate next, IRouteBuilder routeBuilder ) { | |
_routeBuilder = routeBuilder; | |
} | |
public async Task Invoke( HttpContext context ) { | |
try { | |
var globals = _routeBuilder?.Routes?.Where( r => r.GetType() != typeof( AttributeRoute ) ).Select( r => { | |
Route _r = ( (Route)( r ) ); | |
return new { | |
_r.Name, | |
Template = _r.RouteTemplate, | |
DefaultAction = _r.Defaults["action"], | |
DefaultController = _r.Defaults["controller"], | |
}; | |
} ); | |
IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider = context.RequestServices.GetRequiredService<IActionDescriptorCollectionProvider>(); | |
var actions = _actionDescriptorCollectionProvider.ActionDescriptors.Items.Select( a => new { | |
Action = a.RouteValues["action"], | |
Controller = a.RouteValues["controller"], | |
Name = a?.AttributeRouteInfo?.Name, | |
Templates = new string[] { a?.AttributeRouteInfo?.Template }, | |
HttpMethods = a?.ActionConstraints?.OfType<HttpMethodActionConstraint>().FirstOrDefault()?.HttpMethods, | |
Authorized = a?.GetCustomAttributes<AuthorizeAttribute>().Any() | |
} ); | |
context.Response.ContentType = "text/json"; | |
await context.Response.WriteAsync( JsonConvert.SerializeObject( new { globals, actions }, Formatting.Indented ) ); | |
return; | |
} catch ( Exception e ) { | |
await context.Response.WriteAsync( $"{e.Message}" ); | |
return; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment