Skip to content

Instantly share code, notes, and snippets.

@beyond-code-github
Last active December 14, 2015 16:29
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 beyond-code-github/5115938 to your computer and use it in GitHub Desktop.
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
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