Skip to content

Instantly share code, notes, and snippets.

@miguelludert
Created December 29, 2016 21:20
Show Gist options
  • Save miguelludert/6723d5139fb296843f6cefb9e6c9d8e0 to your computer and use it in GitHub Desktop.
Save miguelludert/6723d5139fb296843f6cefb9e6c9d8e0 to your computer and use it in GitHub Desktop.
Unit Testing Routes
public static void MapMvcAttributeRoutes(this RouteCollection routeCollection, Assembly controllerAssembly)
{
var controllerTypes = (from type in controllerAssembly.GetExportedTypes()
where
type != null && type.IsPublic
&& type.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)
&& !type.IsAbstract && typeof(IController).IsAssignableFrom(type)
select type).ToList();
var attributeRoutingAssembly = typeof(RouteCollectionAttributeRoutingExtensions).Assembly;
var attributeRoutingMapperType =
attributeRoutingAssembly.GetType("System.Web.Mvc.Routing.AttributeRoutingMapper");
var mapAttributeRoutesMethod = attributeRoutingMapperType.GetMethod(
"MapAttributeRoutes",
BindingFlags.Public | BindingFlags.Static,
null,
new[] { typeof(RouteCollection), typeof(IEnumerable<Type>) },
null);
mapAttributeRoutesMethod.Invoke(null, new object[] { routeCollection, controllerTypes });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment