Last active
September 2, 2018 19:35
-
-
Save spottedmahn/a2e45f4ebb098bf337eac56fb3817aed to your computer and use it in GitHub Desktop.
Some code to see what ASP.NET Core sees for routes and actions
This file contains hidden or 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.Mvc.ApplicationParts; | |
using Microsoft.AspNetCore.Mvc.Controllers; | |
using Microsoft.AspNetCore.Mvc.Infrastructure; | |
public SomeController( | |
IActionDescriptorCollectionProvider actionDescriptorCollectionProvider, | |
ApplicationPartManager applicationPartManager) | |
{ } | |
[Route("routes")] | |
[HttpGet] | |
//source: https://github.com/ardalis/AspNetCoreRouteDebugger/blob/master/SampleProject/Controllers/RoutesController.cs | |
public IActionResult RoutesDeug() | |
{ | |
var routes = actionDescriptorCollectionProvider.ActionDescriptors.Items | |
.Where(ad => ad is ControllerActionDescriptor) | |
.Cast<ControllerActionDescriptor>() | |
//.Where(cad => cad.ControllerTypeInfo.FullName.Contains("Plugin.Blah")) | |
.Where(cad => (cad.ControllerName == "Category" && cad.ActionName == "Index") | |
|| (cad.ControllerName == "CustomerBlah" && cad.ActionName == "Index") | |
).ToList(); | |
var res = routes | |
.Select(x => new | |
{ | |
Action = x.RouteValues["Action"], | |
Controller = x.RouteValues["Controller"], | |
Name = x.AttributeRouteInfo?.Name, | |
Template = x.AttributeRouteInfo?.Template, | |
Contraint = x.ActionConstraints | |
}).ToList(); | |
return Ok(res); | |
} | |
[Route("controllers")] | |
[HttpGet] | |
//source: https://docs.microsoft.com/en-us/aspnet/core/mvc/advanced/app-parts?view=aspnetcore-2.1#sample-display-available-features | |
public IActionResult ControllersDeug() | |
{ | |
var controllerFeature = new ControllerFeature(); | |
applicationPartManager.PopulateFeature(controllerFeature); | |
var controllers = controllerFeature.Controllers | |
.Where(ctrl => ctrl.FullName.Contains("Plugin.Blah")) | |
.OrderBy(ctrl => ctrl.FullName) | |
.ToList(); | |
return Ok(controllers); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment