/// <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);
        }
Exemplo n.º 2
0
        /// <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);
        }