/// <summary> /// Creates routes for the specified root controller and all other controllers /// in the same namespace or any sub-namespace, in the same assembly, and prepends the /// provided base route to the URL of each created route. /// </summary> /// <param name="routes">A collection of routes for the application.</param> /// <param name="baseRoute">A base route to prepend to the URL of each created route. This parameter can be null.</param> /// <param name="rootController">The root controller for the provided base route.</param> /// <param name="settings">A settings object that customizes the route creation process. This parameter can be null.</param> /// <returns>The created routes.</returns> public static ICollection <Route> MapCodeRoutes(this RouteCollection routes, string baseRoute, Type rootController, CodeRoutingSettings settings) { if (routes == null) { throw new ArgumentNullException("routes"); } if (rootController == null) { throw new ArgumentNullException("rootController"); } var registerSettings = new RegisterSettings(null, rootController) { BaseRoute = baseRoute, Settings = settings }; Route[] newRoutes = RouteFactory.CreateRoutes <Route>(registerSettings); foreach (Route route in newRoutes) { routes.Add(route); } if (newRoutes.Length > 0 && registerSettings.Settings.EnableEmbeddedViews) { EmbeddedViewsVirtualPathProvider.RegisterAssembly(registerSettings); } return(newRoutes); }
public static TRoute[] CreateRoutes <TRoute>(RegisterSettings registerSettings) where TRoute : class { ActionInfo[] actions = registerSettings.GetControllers() .SelectMany(c => c.Actions) .ToArray(); CheckNoAmbiguousUrls(actions); var groupedActions = GroupActions(actions); object config = registerSettings.Settings.Configuration; var routes = new List <TRoute>(); foreach (var group in groupedActions) { ControllerInfo controller = group.First().Controller; RouteFactory routeFactory = controller.Provider.RouteFactory; RouteSettings routeSettings = routeFactory.CreateRouteSettings(group); if (config != null) { routeSettings.DataTokens[DataTokenKeys.Configuration] = config; } object route = routeFactory.CreateRoute(routeSettings, registerSettings); if (route is TRoute) { routes.Add((TRoute)route); } else { TRoute convertedRoute = routeFactory.ConvertRoute(route, typeof(TRoute), registerSettings) as TRoute; if (convertedRoute != null) { routes.Add(convertedRoute); } // TODO: else, throw exception? } } return(routes.ToArray()); }
/// <summary> /// Creates routes for the specified root controller and all other controllers /// in the same namespace or any sub-namespace, in the same assembly, and prepends the /// provided base route to the URL of each created route. /// </summary> /// <param name="configuration">The <see cref="System.Web.Http.HttpConfiguration"/> configuration object.</param> /// <param name="baseRoute">A base route to prepend to the URL of each created route. This parameter can be null.</param> /// <param name="rootController">The root controller for the provided base route.</param> /// <param name="settings">A settings object that customizes the route creation process. This parameter can be null.</param> /// <returns>The created routes.</returns> public static ICollection <IHttpRoute> MapCodeRoutes(this HttpConfiguration configuration, string baseRoute, Type rootController, CodeRoutingSettings settings) { if (configuration == null) { throw new ArgumentNullException("configuration"); } if (rootController == null) { throw new ArgumentNullException("rootController"); } if (settings != null) { settings = new CodeRoutingSettings(settings); } var registerSettings = new RegisterSettings(null, rootController) { BaseRoute = baseRoute, Settings = settings }; registerSettings.Settings.HttpConfiguration(configuration); IHttpRoute[] newRoutes = RouteFactory.CreateRoutes <IHttpRoute>(registerSettings); foreach (IHttpRoute route in newRoutes) { // In Web API v1 name cannot be null configuration.Routes.Add(Guid.NewGuid().ToString(), route); } EnableCodeRouting(configuration); return(newRoutes); }