Ejemplo n.º 1
0
        public async Task SelectAsync_WithCatchAll_NoMatch()
        {
            // Arrange
            var actions = new ActionDescriptor[]
            {
                CreateAction(area: null, controller: "Store", action: "Buy"),
                CreateAction(area: null, controller: "Store", action: "Buy"),
                CreateAction(area: null, controller: "Store", action: "Buy"),
            };

            actions[0].RouteConstraints.Add(new RouteDataActionConstraint("country", "CA"));
            actions[1].RouteConstraints.Add(new RouteDataActionConstraint("country", "US"));
            actions[2].RouteConstraints.Add(RouteDataActionConstraint.CreateCatchAll("country"));

            var selector = CreateSelector(actions);
            var context  = CreateRouteContext("GET");

            context.RouteData.Values.Add("controller", "Store");
            context.RouteData.Values.Add("action", "Buy");

            // Act
            var action = await selector.SelectAsync(context);

            // Assert
            Assert.Null(action);
        }
        private static void AddControllerRouteConstraints(
            ControllerActionDescriptor actionDescriptor,
            IList <RouteConstraintAttribute> routeconstraints,
            ISet <string> removalConstraints)
        {
            // Apply all the constraints defined on the controller (for example, [Area]) to the actions
            // in that controller. Also keep track of all the constraints that require preventing actions
            // without the constraint to match. For example, actions without an [Area] attribute on their
            // controller should not match when a value has been given for area when matching a url or
            // generating a link.
            foreach (var constraintAttribute in routeconstraints)
            {
                if (constraintAttribute.BlockNonAttributedActions)
                {
                    removalConstraints.Add(constraintAttribute.RouteKey);
                }

                // Skip duplicates
                if (!HasConstraint(actionDescriptor.RouteConstraints, constraintAttribute.RouteKey))
                {
                    if (constraintAttribute.RouteKeyHandling == RouteKeyHandling.CatchAll)
                    {
                        actionDescriptor.RouteConstraints.Add(
                            RouteDataActionConstraint.CreateCatchAll(
                                constraintAttribute.RouteKey));
                    }
                    else
                    {
                        actionDescriptor.RouteConstraints.Add(new RouteDataActionConstraint(
                                                                  constraintAttribute.RouteKey,
                                                                  constraintAttribute.RouteValue));
                    }
                }
            }
        }
        /// <summary>
        /// Create a catch all constraint for the given key.
        /// </summary>
        /// <param name="routeKey">Route key.</param>
        /// <returns>a <see cref="RouteDataActionConstraint"/> that represents a catch all constraint.</returns>
        public static RouteDataActionConstraint CreateCatchAll(string routeKey)
        {
            var c = new RouteDataActionConstraint(routeKey);
            c.KeyHandling = RouteKeyHandling.CatchAll;
            c.RouteValue = string.Empty;

            return c;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create a catch all constraint for the given key.
        /// </summary>
        /// <param name="routeKey">Route key.</param>
        /// <returns>a <see cref="RouteDataActionConstraint"/> that represents a catch all constraint.</returns>
        public static RouteDataActionConstraint CreateCatchAll(string routeKey)
        {
            var c = new RouteDataActionConstraint(routeKey);

            c.KeyHandling = RouteKeyHandling.CatchAll;
            c.RouteValue  = string.Empty;

            return(c);
        }
Ejemplo n.º 5
0
        public static void AddRouteConstraints(
            ISet <string> removalConstraints,
            ControllerActionDescriptor actionDescriptor,
            ControllerModel controller,
            ActionModel action)
        {
            // Apply all the constraints defined on the action, then controller (for example, [Area])
            // to the actions. Also keep track of all the constraints that require preventing actions
            // without the constraint to match. For example, actions without an [Area] attribute on their
            // controller should not match when a value has been given for area when matching a url or
            // generating a link.
            foreach (var constraintAttribute in action.RouteConstraints)
            {
                if (constraintAttribute.BlockNonAttributedActions)
                {
                    removalConstraints.Add(constraintAttribute.RouteKey);
                }

                // Skip duplicates
                if (!HasConstraint(actionDescriptor.RouteConstraints, constraintAttribute.RouteKey))
                {
                    if (constraintAttribute.RouteKeyHandling == RouteKeyHandling.CatchAll)
                    {
                        actionDescriptor.RouteConstraints.Add(
                            RouteDataActionConstraint.CreateCatchAll(
                                constraintAttribute.RouteKey));
                    }
                    else
                    {
                        actionDescriptor.RouteConstraints.Add(new RouteDataActionConstraint(
                                                                  constraintAttribute.RouteKey,
                                                                  constraintAttribute.RouteValue));
                    }
                }
            }

            foreach (var constraintAttribute in controller.RouteConstraints)
            {
                if (constraintAttribute.BlockNonAttributedActions)
                {
                    removalConstraints.Add(constraintAttribute.RouteKey);
                }

                // Skip duplicates - this also means that a value on the action will take precedence
                if (!HasConstraint(actionDescriptor.RouteConstraints, constraintAttribute.RouteKey))
                {
                    if (constraintAttribute.RouteKeyHandling == RouteKeyHandling.CatchAll)
                    {
                        actionDescriptor.RouteConstraints.Add(
                            RouteDataActionConstraint.CreateCatchAll(
                                constraintAttribute.RouteKey));
                    }
                    else
                    {
                        actionDescriptor.RouteConstraints.Add(new RouteDataActionConstraint(
                                                                  constraintAttribute.RouteKey,
                                                                  constraintAttribute.RouteValue));
                    }
                }
            }

            // Lastly add the 'default' values
            if (!HasConstraint(actionDescriptor.RouteConstraints, "action"))
            {
                actionDescriptor.RouteConstraints.Add(new RouteDataActionConstraint(
                                                          "action",
                                                          action.ActionName ?? string.Empty));
            }

            if (!HasConstraint(actionDescriptor.RouteConstraints, "controller"))
            {
                actionDescriptor.RouteConstraints.Add(new RouteDataActionConstraint(
                                                          "controller",
                                                          controller.ControllerName));
            }
        }