Skip to content

Instantly share code, notes, and snippets.

@spottedmahn
Last active September 2, 2018 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spottedmahn/a2e45f4ebb098bf337eac56fb3817aed to your computer and use it in GitHub Desktop.
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
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