Last active
December 14, 2015 16:29
-
-
Save beyond-code-github/5115938 to your computer and use it in GitHub Desktop.
Quick sketch of pulling together data required to build nested routes
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
var timer = new Stopwatch(); | |
timer.Start(); | |
var configuration = GlobalConfiguration.Configuration; | |
var explorer = new ApiExplorer(configuration); | |
var descriptions = explorer.ApiDescriptions; | |
var controllerDescriptors = descriptions.Select(o => o.ActionDescriptor.ControllerDescriptor).Distinct().ToList(); | |
var nestedController = | |
controllerDescriptors.Where(o => typeof(INestedApiController).IsAssignableFrom(o.ControllerType)) | |
.Select( | |
o => | |
new | |
{ | |
Descriptor = o, | |
NestedUnder = FindRelatedControllerDescriptor(controllerDescriptors, o), | |
NestedDescription = descriptions.Where(d => d.ActionDescriptor.ControllerDescriptor == FindRelatedControllerDescriptor(controllerDescriptors, o)).ToList(), | |
ParentRoutes = descriptions.Where(d => d.ActionDescriptor.ControllerDescriptor == FindRelatedControllerDescriptor(controllerDescriptors, o)).Select(d => d.Route).Distinct().ToList(), | |
}).First(); | |
var routeName = nestedController.Descriptor.ControllerName + "-" | |
+ nestedController.NestedUnder.ControllerName; | |
var routes = nestedController.NestedUnder.Configuration.Routes; | |
/*----------------*/ | |
private static HttpControllerDescriptor FindRelatedControllerDescriptor(IEnumerable<HttpControllerDescriptor> controllerDescriptors, HttpControllerDescriptor childDescriptor) | |
{ | |
return | |
controllerDescriptors.FirstOrDefault( | |
cd => | |
cd.ControllerType | |
== childDescriptor.ControllerType.GetInterfaces() | |
.First(i => i.GenericTypeArguments.Any()) | |
.GenericTypeArguments.FirstOrDefault()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment