/// <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 override object ConvertRoute(object route, Type conversionType, RegisterSettings registerSettings)
        {
            Func<object, RegisterSettings, object> converterFn;

             if (!RouteConverters.TryGetValue(conversionType, out converterFn)) {
            return base.ConvertRoute(route, conversionType, registerSettings);
             }

             return converterFn(route, registerSettings);
        }
        public override object ConvertRoute(object route, Type conversionType, RegisterSettings registerSettings)
        {
            Func <object, RegisterSettings, object> converterFn;

            if (!RouteConverters.TryGetValue(conversionType, out converterFn))
            {
                return(base.ConvertRoute(route, conversionType, registerSettings));
            }

            return(converterFn(route, registerSettings));
        }
Example #4
0
        public static ControllerInfo AnalyzeControllerType(Type controllerType, RegisterSettings registerSettings)
        {
            foreach (CodeRoutingProvider provider in providers)
            {
                if (provider.SupportsControllerType(controllerType))
                {
                    return(provider.CreateControllerInfo(controllerType, registerSettings));
                }
            }

            return(null);
        }
        public override object CreateRoute(RouteSettings routeSettings, RegisterSettings registerSettings)
        {
            var defaults = new HttpRouteValueDictionary(routeSettings.Defaults);
             var constraints = new HttpRouteValueDictionary(routeSettings.Constraints);
             var dataTokens = new HttpRouteValueDictionary(routeSettings.DataTokens);

             return new CodeHttpRoute(routeSettings.RouteTemplate, defaults, constraints, dataTokens) {
            ActionMapping = routeSettings.ActionMapping,
            ControllerMapping = routeSettings.ControllerMapping,
            ControllerDescriptors = routeSettings.Actions
               .Select(a => a.Controller)
               .DistinctReference()
               .ToDictionary(c => c.Name, c => ((DescribedHttpControllerInfo)c).Descriptor, StringComparer.OrdinalIgnoreCase)
             };
        }
        public override object CreateRoute(RouteSettings routeSettings, RegisterSettings registerSettings)
        {
            var defaults    = new HttpRouteValueDictionary(routeSettings.Defaults);
            var constraints = new HttpRouteValueDictionary(routeSettings.Constraints);
            var dataTokens  = new HttpRouteValueDictionary(routeSettings.DataTokens);

            return(new CodeHttpRoute(routeSettings.RouteTemplate, defaults, constraints, dataTokens)
            {
                ActionMapping = routeSettings.ActionMapping,
                ControllerMapping = routeSettings.ControllerMapping,
                ControllerDescriptors = routeSettings.Actions
                                        .Select(a => a.Controller)
                                        .DistinctReference()
                                        .ToDictionary(c => c.Name, c => ((DescribedHttpControllerInfo)c).Descriptor, StringComparer.OrdinalIgnoreCase)
            });
        }
Example #7
0
        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());
        }
Example #8
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);
        }
Example #9
0
 protected abstract ControllerInfo CreateControllerInfo(Type controllerType, RegisterSettings registerSettings);
 protected override ControllerInfo CreateControllerInfo(Type controllerType, RegisterSettings registerSettings)
 {
     return HttpControllerInfo.Create(controllerType, registerSettings, this);
 }
        /// <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;
        }
      /// <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;
      }
Example #13
0
 public virtual object ConvertRoute(object route, Type conversionType, RegisterSettings registerSettings)
 {
     return(null);
 }
Example #14
0
 public abstract object CreateRoute(RouteSettings routeSettings, RegisterSettings registerSettings);
 protected override ControllerInfo CreateControllerInfo(Type controllerType, RegisterSettings registerSettings)
 {
     return(HttpControllerInfo.Create(controllerType, registerSettings, this));
 }