Exemple #1
0
        private static HttpRouteCollection MapHttpAttributeRoutesInternal(this HttpConfiguration configuration, HttpRouteBuilder routeBuilder)
        {
            HttpRouteCollection subRoutes = new HttpRouteCollection();

            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.Actions);
                    }

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

                attributeRoutes.Sort();

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

            return(subRoutes);
        }
        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);
        }