コード例 #1
0
        /// <summary>
        /// Validates that a constraint is valid for an <see cref="IHttpRoute"/> created by a call
        /// to the <see cref="HttpRouteCollection.CreateRoute(string, IDictionary&lt;string, object&gt;, IDictionary&lt;string, object&gt;, IDictionary&lt;string, object&gt;, HttpMessageHandler)"/> method.
        /// </summary>
        /// <param name="routeTemplate">The route template.</param>
        /// <param name="name">The constraint name.</param>
        /// <param name="constraint">The constraint object.</param>
        /// <remarks>
        /// Implement this method when deriving from <see cref="HttpRouteCollection"/> to allow contraints of
        /// types other than <see cref="string"/> and <see cref="IHttpRouteConstraint"/>.
        /// </remarks>
        protected virtual void ValidateConstraint(string routeTemplate, string name, object constraint)
        {
            if (name == null)
            {
                throw Error.ArgumentNull("name");
            }

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

            HttpRoute.ValidateConstraint(routeTemplate, name, constraint);
        }
コード例 #2
0
        /// <inheritdoc/>
        public virtual RouteEntry Build()
        {
            if (ParsedRoute == null)
            {
                ParsedRoute = RouteParser.Parse(Template);
            }

            ValidateParameters(ParsedRoute);

            TRouteDictionaryConcrete defaults;

#if ASPNETWEBAPI
            defaults = Copy(Defaults);
#else
            defaults = Copy(Defaults) ?? new RouteValueDictionary();
#endif
            TRouteDictionaryConcrete constraints = Copy(Constraints);
            TRouteDictionaryConcrete dataTokens  =
                Copy(DataTokens) ?? new TRouteDictionaryConcrete();

            dataTokens[RouteDataTokenKeys.Actions] = _actions;

#if ASPNETWEBAPI
            if (!TargetIsAction)
            {
                dataTokens[RouteDataTokenKeys.Controller] = _actions[0].ControllerDescriptor;
            }
#endif

            int order = Order;

            if (order != default(int))
            {
                dataTokens[RouteDataTokenKeys.Order] = order;
            }

            decimal precedence = Precedence;

            if (precedence != default(decimal))
            {
                dataTokens[RouteDataTokenKeys.Precedence] = precedence;
            }

#if ASPNETWEBAPI
            if (constraints != null)
            {
                foreach (var constraint in constraints)
                {
                    HttpRoute.ValidateConstraint(Template, constraint.Key, constraint.Value);
                }
            }

            HttpMessageHandler handler = null;
            IHttpRoute         route   = new HttpRoute(
                Template,
                defaults,
                constraints,
                dataTokens,
                handler,
                ParsedRoute
                );
#else
            ControllerDescriptor controllerDescriptor = GetControllerDescriptor();

            if (controllerDescriptor != null)
            {
                defaults["controller"] = controllerDescriptor.ControllerName;
            }

            if (TargetIsAction && _actions.Length == 1)
            {
                ActionDescriptor actionDescriptor = _actions[0];
                defaults["action"] = actionDescriptor.ActionName;

                dataTokens[RouteDataTokenKeys.TargetIsAction] = true;
            }

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

            if (areaName != null)
            {
                dataTokens[RouteDataTokenKeys.Area] = areaName;
                dataTokens[RouteDataTokenKeys.UseNamespaceFallback] = false;

                Type controllerType = controllerDescriptor.ControllerType;

                if (controllerType != null)
                {
                    dataTokens[RouteDataTokenKeys.Namespaces] = new[] { controllerType.Namespace };
                }
            }

            Route route = new Route(
                Template,
                defaults,
                constraints,
                dataTokens,
                routeHandler: null
                );

            ConstraintValidation.Validate(route);
#endif

            return(new RouteEntry(Name, route));
        }