/// <summary>
        /// Maps the attribute-defined routes for the application.
        /// </summary>
        /// <param name="configuration">The server configuration.</param>
        /// <param name="routeBuilder">The <see cref="HttpRouteBuilder"/> to use for generating attribute routes.</param>
        public static void MapHttpAttributeRoutes(this HttpConfiguration configuration, HttpRouteBuilder routeBuilder)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            if (routeBuilder == null)
            {
                throw Error.ArgumentNull("routeBuilder");
            }

            List<HttpRouteEntry> attributeRoutes = new List<HttpRouteEntry>();

            IHttpControllerSelector controllerSelector = configuration.Services.GetHttpControllerSelector();
            IHttpActionSelector actionSelector = configuration.Services.GetActionSelector();
            foreach (HttpControllerDescriptor controllerDescriptor in controllerSelector.GetControllerMapping().Values)
            {
                Collection<RoutePrefixAttribute> routePrefixes = controllerDescriptor.GetCustomAttributes<RoutePrefixAttribute>(inherit: false);

                foreach (IGrouping<string, HttpActionDescriptor> actionGrouping in actionSelector.GetActionMapping(controllerDescriptor))
                {
                    string controllerName = controllerDescriptor.ControllerName;
                    attributeRoutes.AddRange(CreateAttributeRoutes(routeBuilder, controllerName, routePrefixes, actionGrouping));
                }
            }

            attributeRoutes.Sort();

            foreach (HttpRouteEntry attributeRoute in attributeRoutes)
            {
                configuration.Routes.Add(attributeRoute.Name, attributeRoute.Route);
            }
        }
        /// <summary>
        /// Maps the attribute-defined routes for the application.
        /// </summary>
        /// <param name="configuration">The server configuration.</param>
        /// <param name="constraintResolver">The <see cref="IInlineConstraintResolver"/> to use for resolving inline constraints.</param>
        public static void MapHttpAttributeRoutes(this HttpConfiguration configuration, IInlineConstraintResolver constraintResolver)
        {
            HttpRouteBuilder routeBuilder = new HttpRouteBuilder(constraintResolver);
            var attrRoute = new RouteCollectionRoute();
            configuration.Routes.Add(AttributeRouteName, attrRoute);

            Action<HttpConfiguration> previousInitializer = configuration.Initializer;
            configuration.Initializer = config =>
                {
                    // Chain to the previous initializer hook. Do this before we access the config since
                    // initialization may make last minute changes to the configuration.
                    previousInitializer(config);

                    // Add a single placeholder route that handles all of attribute routing.
                    // Add an initialize hook that initializes these routes after the config has been initialized.
                    Func<HttpSubRouteCollection> initializer = () => MapHttpAttributeRoutesInternal(configuration, routeBuilder);

                    // This won't change config. It wants to pick up the finalized config.
                    HttpSubRouteCollection subRoutes = attrRoute.EnsureInitialized(initializer);
                    if (subRoutes != null)
                    {
                        AddGenerationHooksForSubRoutes(config.Routes, subRoutes, routeBuilder);
                    }
                };
        }
Beispiel #3
0
        private static HttpRouteEntry CreateRouteEntry(string routeTemplate)
        {
            IHttpRoute route = new HttpRouteBuilder().BuildHttpRoute(routeTemplate, new HttpMethod[] { HttpMethod.Get }, "Controller", "Action");

            return(new HttpRouteEntry()
            {
                Route = route, RouteTemplate = routeTemplate
            });
        }
Beispiel #4
0
        public void Ctor_SetsActionsDataToken()
        {
            var actions = new ReflectedHttpActionDescriptor[0];

            var route = HttpRouteBuilder.BuildDirectRoute("route", 0, actions);

            var actualActions = route.DataTokens[RouteKeys.ActionsDataTokenKey];

            Assert.Equal(actions, actualActions);
        }
Beispiel #5
0
        public void GetRouteData_AddsDefaultValuesAsOptional()
        {
            var actions = new ReflectedHttpActionDescriptor[] { new ReflectedHttpActionDescriptor() };
            var route   = HttpRouteBuilder.BuildDirectRoute("movies/{id}", 0, actions);

            route.Defaults.Add("id", RouteParameter.Optional);

            var routeData = route.GetRouteData("", new HttpRequestMessage(HttpMethod.Get, "http://localhost/movies"));

            Assert.Equal(RouteParameter.Optional, routeData.Values["id"]);
        }
Beispiel #6
0
        private static IHttpRoute BuildRoute(string routeTemplate, IInlineConstraintResolver constraintResolver = null)
        {
            // Act
            HttpRouteBuilder routeBuilder = new HttpRouteBuilder(constraintResolver ?? new DefaultInlineConstraintResolver());
            IHttpRoute       route        = routeBuilder.BuildHttpRoute(routeTemplate, new HttpMethod[] { HttpMethod.Get }, "FakeController", "FakeAction");

            // Assertions for default, unspecified behavior:
            Assert.NotNull(route);
            Assert.Equal("FakeController", route.Defaults["controller"]);
            Assert.Equal("FakeAction", route.Defaults["action"]);
            Assert.IsType <HttpMethodConstraint>(route.Constraints["httpMethod"]);

            return(route);
        }
        private static IHttpRoute BuildRoute(string routeTemplate, IInlineConstraintResolver constraintResolver = null)
        {
            ReflectedHttpActionDescriptor[] actions = new ReflectedHttpActionDescriptor[0];

            // Act
            HttpRouteBuilder routeBuilder = new HttpRouteBuilder(constraintResolver ?? new DefaultInlineConstraintResolver());
            IHttpRoute       route        = routeBuilder.BuildParsingRoute(routeTemplate, 0, actions: actions);

            // Assertions for default, unspecified behavior:
            Assert.NotNull(route);
            Assert.Same(actions, route.DataTokens["actions"]);

            return(route);
        }
        private static IHttpRoute BuildRoute(string routeTemplate, IInlineConstraintResolver constraintResolver = null)
        {
            ReflectedHttpActionDescriptor[] actions = new ReflectedHttpActionDescriptor[0];

            // Act
            HttpRouteBuilder routeBuilder = new HttpRouteBuilder(constraintResolver ?? new DefaultInlineConstraintResolver());
            IHttpRoute route = routeBuilder.BuildHttpRoute(routeTemplate, actions: actions);

            // Assertions for default, unspecified behavior:
            Assert.NotNull(route);
            Assert.Same(actions, route.DataTokens["actions"]);
            
            return route;
        }
        /// <summary>
        /// Maps the attribute-defined routes for the application.
        /// </summary>
        /// <param name="configuration">The server configuration.</param>
        /// <param name="routeBuilder">The <see cref="HttpRouteBuilder"/> to use for generating attribute routes.</param>
        public static void MapHttpAttributeRoutes(this HttpConfiguration configuration, HttpRouteBuilder routeBuilder)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            if (routeBuilder == null)
            {
                throw Error.ArgumentNull("routeBuilder");
            }

            List<HttpRouteEntry> attributeRoutes = new List<HttpRouteEntry>();

            IHttpControllerSelector controllerSelector = configuration.Services.GetHttpControllerSelector();
            IDictionary<string, HttpControllerDescriptor> controllerMap = controllerSelector.GetControllerMapping();
            if (controllerMap != null)
            {
                foreach (HttpControllerDescriptor controllerDescriptor in controllerMap.Values)
                {
                    IEnumerable<HttpRouteEntry> controllerRoutes = CreateRouteEntries(controllerDescriptor);

                    foreach (HttpRouteEntry route in controllerRoutes)
                    {
                        route.Route = routeBuilder.BuildHttpRoute(route.RouteTemplate, route.HttpMethods, route.Actions);
                    }

                    SetDefaultRouteNames(controllerRoutes, controllerDescriptor.ControllerName);
                    attributeRoutes.AddRange(controllerRoutes);
                }

                attributeRoutes.Sort();

                foreach (HttpRouteEntry attributeRoute in attributeRoutes)
                {
                    IHttpRoute route = attributeRoute.Route;
                    if (route != null)
                    {
                        configuration.Routes.Add(attributeRoute.Name, attributeRoute.Route);
                    }
                }
            }
        }
        private static List<HttpRouteEntry> CreateAttributeRoutes(HttpRouteBuilder routeBuilder, string controllerName,
            Collection<RoutePrefixAttribute> routePrefixes, IGrouping<string, HttpActionDescriptor> actionGrouping)
        {
            List<HttpRouteEntry> routes = new List<HttpRouteEntry>();
            string actionName = actionGrouping.Key;

            foreach (HttpActionDescriptor actionDescriptor in actionGrouping)
            {
                IEnumerable<IHttpRouteInfoProvider> routeInfoProviders = actionDescriptor.GetCustomAttributes<IHttpRouteInfoProvider>(inherit: false);

                // DefaultIfEmpty below is required to add routes when there is a route prefix but no
                // route provider or when there is a route provider with a template but no route prefix
                foreach (RoutePrefixAttribute routePrefix in routePrefixes.DefaultIfEmpty())
                {
                    foreach (IHttpRouteInfoProvider routeProvider in routeInfoProviders.DefaultIfEmpty())
                    {
                        string prefixTemplate = routePrefix == null ? null : routePrefix.Prefix;
                        string providerTemplate = routeProvider == null ? null : routeProvider.RouteTemplate;
                        if (prefixTemplate == null && providerTemplate == null)
                        {
                            continue;
                        }

                        ValidateTemplates(prefixTemplate, providerTemplate, actionDescriptor);

                        string routeTemplate;
                        if (String.IsNullOrEmpty(prefixTemplate))
                        {
                            routeTemplate = providerTemplate ?? String.Empty;
                        }
                        else if (String.IsNullOrEmpty(providerTemplate))
                        {
                            routeTemplate = prefixTemplate;
                        }
                        else
                        {
                            // template and prefix both not null - combine them
                            routeTemplate = prefixTemplate + '/' + providerTemplate;
                        }

                        Collection<HttpMethod> httpMethods = actionDescriptor.SupportedHttpMethods;
                        IHttpRoute route = routeBuilder.BuildHttpRoute(routeTemplate, httpMethods, controllerName, actionName);
                        HttpRouteEntry entry = new HttpRouteEntry() { Route = route, RouteTemplate = routeTemplate };
                        if (routeProvider != null)
                        {
                            entry.Name = routeProvider.RouteName;
                            entry.Order = routeProvider.RouteOrder;
                        }
                        if (routePrefix != null)
                        {
                            entry.PrefixOrder = routePrefix.Order;
                        }
                        routes.Add(entry);
                    }
                }
            }

            SetDefaultRouteNames(routes, controllerName, actionName);

            return routes;
        }
        private static HttpSubRouteCollection MapHttpAttributeRoutesInternal(this HttpConfiguration configuration, HttpRouteBuilder routeBuilder)
        {
            HttpSubRouteCollection subRoutes = new HttpSubRouteCollection();

            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            if (routeBuilder == null)
            {
                throw Error.ArgumentNull("routeBuilder");
            }

            List<HttpRouteEntry> attributeRoutes = new List<HttpRouteEntry>();

            IHttpControllerSelector controllerSelector = configuration.Services.GetHttpControllerSelector();
            IDictionary<string, HttpControllerDescriptor> controllerMap = controllerSelector.GetControllerMapping();
            if (controllerMap != null)
            {
                foreach (HttpControllerDescriptor controllerDescriptor in controllerMap.Values)
                {
                    IEnumerable<HttpRouteEntry> controllerRoutes = CreateRouteEntries(controllerDescriptor);

                    foreach (HttpRouteEntry route in controllerRoutes)
                    {
                        route.Route = routeBuilder.BuildParsingRoute(route.Template, route.Order, route.Actions);
                    }

                    attributeRoutes.AddRange(controllerRoutes);
                }

                foreach (HttpRouteEntry attributeRoute in attributeRoutes)
                {
                    IHttpRoute route = attributeRoute.Route;
                    if (route != null)
                    {
                        subRoutes.Add(attributeRoute.Name, attributeRoute.Route);
                    }
                }
            }

            return subRoutes;
        }
 // Add generation hooks for the Attribute-routing subroutes. 
 // This lets us generate urls for routes supplied by attr-based routing.
 private static void AddGenerationHooksForSubRoutes(HttpRouteCollection destRoutes, HttpSubRouteCollection sourceRoutes, HttpRouteBuilder routeBuilder)
 {
     foreach (KeyValuePair<string, IHttpRoute> kv in sourceRoutes.NamedRoutes)
     {
         string name = kv.Key;
         IHttpRoute route = kv.Value;
         var stubRoute = routeBuilder.BuildGenerationRoute(route);
         destRoutes.Add(name, stubRoute);
     }
 }
 private static HttpRouteEntry CreateRouteEntry(string routeTemplate)
 {
     IHttpRoute route = new HttpRouteBuilder().BuildHttpRoute(routeTemplate, new HttpMethod[] { HttpMethod.Get }, "Controller", "Action");
     return new HttpRouteEntry() { Route = route, RouteTemplate = routeTemplate };
 }