Beispiel #1
0
        /// <summary>
        /// Builds an <see cref="Route"/> for a particular controller.
        /// </summary>
        /// <param name="routeTemplate">The tokenized route template for the route.</param>
        /// <param name="controllerDescriptor">The controller the route attribute has been applied on.</param>
        /// <returns>The generated <see cref="Route"/>.</returns>
        public Route BuildDirectRoute(
            string routeTemplate,
            ControllerDescriptor controllerDescriptor
            )
        {
            if (routeTemplate == null)
            {
                throw Error.ArgumentNull("routeTemplate");
            }

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

            string controllerName = controllerDescriptor.ControllerName;

            RouteAreaAttribute area     = controllerDescriptor.GetAreaFrom();
            string             areaName = controllerDescriptor.GetAreaName(area);

            RouteValueDictionary defaults = new RouteValueDictionary
            {
                { "controller", controllerName }
            };

            Type controllerType = controllerDescriptor.ControllerType;

            RouteValueDictionary dataTokens = new RouteValueDictionary();

            if (areaName != null)
            {
                dataTokens.Add(RouteDataTokenKeys.Area, areaName);
                dataTokens.Add(RouteDataTokenKeys.UseNamespaceFallback, value: false);
                if (controllerType != null)
                {
                    dataTokens.Add(
                        RouteDataTokenKeys.Namespaces,
                        new[] { controllerType.Namespace }
                        );
                }
            }

            RouteValueDictionary constraints = new RouteValueDictionary();
            string detokenizedRouteTemplate  = InlineRouteTemplateParser.ParseRouteTemplate(
                routeTemplate,
                defaults,
                constraints,
                ConstraintResolver
                );

            Route route = new Route(detokenizedRouteTemplate, new MvcRouteHandler())
            {
                Defaults    = defaults,
                Constraints = constraints,
                DataTokens  = dataTokens
            };

            return(route);
        }
        /// <summary>
        /// Builds an <see cref="Route"/> for a particular action.
        /// </summary>
        /// <param name="routeTemplate">The tokenized route template for the route.</param>
        /// <param name="allowedMethods">The HTTP methods supported by the route. A null value specify that all possible methods are supported.</param>
        /// <param name="controllerName">The name of the associated controller.</param>
        /// <param name="actionName">The name of the associated action.</param>
        /// <param name="targetMethod">The method that the route attribute has been applied on.</param>
        /// <param name="areaName"></param>
        /// <returns>The generated <see cref="Route"/>.</returns>
        public Route BuildDirectRoute(string routeTemplate, IEnumerable <string> allowedMethods, string controllerName, string actionName, MethodInfo targetMethod, string areaName)
        {
            if (routeTemplate == null)
            {
                throw Error.ArgumentNull("routeTemplate");
            }

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

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

            RouteValueDictionary defaults = new RouteValueDictionary
            {
                { "controller", controllerName },
                { "action", actionName }
            };

            RouteValueDictionary constraints = new RouteValueDictionary();

            if (allowedMethods != null)
            {
                string[] array = allowedMethods.ToArray();
                if (array.Length > 0)
                {
                    // Current method constraint implementation is inefficient since it matches before running the constraint.
                    // Consider checking the HTTP method first in a custom route as a performance optimization.
                    constraints.Add("httpMethod", new HttpMethodConstraint(array));
                }
            }

            RouteValueDictionary dataTokens = new RouteValueDictionary();

            if (areaName != null)
            {
                dataTokens.Add(RouteDataTokenKeys.Area, areaName);
                dataTokens.Add(RouteDataTokenKeys.UseNamespaceFallback, value: false);
                if (targetMethod.DeclaringType != null)
                {
                    dataTokens.Add(RouteDataTokenKeys.Namespaces, new[] { targetMethod.DeclaringType.Namespace });
                }
            }

            string detokenizedRouteTemplate = InlineRouteTemplateParser.ParseRouteTemplate(routeTemplate, defaults, constraints, ConstraintResolver);

            return(BuildDirectRoute(defaults, constraints, dataTokens, detokenizedRouteTemplate, targetMethod));
        }
        /// <summary>
        /// Builds an <see cref="Route"/> for a particular controller.
        /// </summary>
        /// <param name="routeTemplate">The tokenized route template for the route.</param>
        /// <param name="routeInfoProvider">The info provider for the route.</param>
        /// <param name="controllerDescriptor">The controller the route attribute has been applied on.</param>
        /// <param name="actionDescriptors">The actions reachable by this route.</param>
        /// <param name="routeIsForAction">Whether or not the direct route is for an action.</param>
        /// <returns>The generated <see cref="Route"/>.</returns>
        public Route BuildDirectRoute(
            string routeTemplate,
            IRouteInfoProvider routeInfoProvider,
            ControllerDescriptor controllerDescriptor,
            IEnumerable <ActionDescriptor> actionDescriptors,
            bool routeIsForAction)
        {
            if (routeTemplate == null)
            {
                throw Error.ArgumentNull("routeTemplate");
            }

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

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

            if (actionDescriptors == null || !actionDescriptors.Any())
            {
                throw Error.ParameterCannotBeNullOrEmpty("actionDescriptors");
            }

            string controllerName = controllerDescriptor.ControllerName;

            RouteAreaAttribute area     = controllerDescriptor.GetAreaFrom();
            string             areaName = controllerDescriptor.GetAreaName(area);

            RouteValueDictionary defaults = new RouteValueDictionary
            {
                { "controller", controllerName }
            };

            if (routeIsForAction)
            {
                ActionDescriptor actionDescriptor = actionDescriptors.Single();
                defaults.Add("action", actionDescriptor.ActionName);
            }

            RouteValueDictionary constraints = new RouteValueDictionary();

            string      detokenizedRouteTemplate = InlineRouteTemplateParser.ParseRouteTemplate(routeTemplate, defaults, constraints, ConstraintResolver);
            ParsedRoute parsedRoute = RouteParser.Parse(detokenizedRouteTemplate);

            RouteValueDictionary dataTokens = new RouteValueDictionary();

            dataTokens[RouteDataTokenKeys.DirectRoutePrecedence] = RoutePrecedence.Compute(parsedRoute, constraints);
            dataTokens[RouteDataTokenKeys.DirectRouteController] = controllerDescriptor;
            dataTokens[RouteDataTokenKeys.DirectRouteActions]    = actionDescriptors;

            int order = 0;
            IOrderedRouteInfoProvider orderedAttribute = routeInfoProvider as IOrderedRouteInfoProvider;

            if (orderedAttribute != null)
            {
                order = orderedAttribute.Order;
            }

            dataTokens[RouteDataTokenKeys.DirectRouteOrder] = order;

            if (areaName != null)
            {
                dataTokens.Add(RouteDataTokenKeys.Area, areaName);
                dataTokens.Add(RouteDataTokenKeys.UseNamespaceFallback, value: false);

                Type controllerType = controllerDescriptor.ControllerType;
                if (controllerType != null)
                {
                    dataTokens.Add(RouteDataTokenKeys.Namespaces, new[] { controllerType.Namespace });
                }
            }

            Route route = new Route(detokenizedRouteTemplate, new MvcRouteHandler())
            {
                Defaults    = defaults,
                Constraints = constraints,
                DataTokens  = dataTokens
            };

            return(route);
        }