public void GetNormalizedRouteValue_ReturnsRouteDataValue_IfRouteConstraintKeyHandlingIsCatchAll() { // Arrange var key = "some-key"; var actionDescriptor = new ActionDescriptor { RouteConstraints = new[] { RouteDataActionConstraint.CreateCatchAll(key) } }; var actionContext = new ActionContext { ActionDescriptor = actionDescriptor, RouteData = new RouteData() }; actionContext.RouteData.Values[key] = "route-value"; // Act var result = RazorViewEngine.GetNormalizedRouteValue(actionContext, key); // Assert Assert.Equal("route-value", result); }
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); }
public void RouteDataActionConstraint_DenyKeyByPassingEmptyString() { var routeDataConstraint = new RouteDataActionConstraint("key", string.Empty); Assert.Equal(routeDataConstraint.RouteKey, "key"); Assert.Equal(routeDataConstraint.KeyHandling, RouteKeyHandling.DenyKey); Assert.Equal(routeDataConstraint.RouteValue, string.Empty); }
public void RouteDataActionConstraint_CatchAll() { var routeDataConstraint = RouteDataActionConstraint.CreateCatchAll("key"); Assert.Equal(routeDataConstraint.RouteKey, "key"); Assert.Equal(routeDataConstraint.KeyHandling, RouteKeyHandling.CatchAll); Assert.Equal(routeDataConstraint.RouteValue, string.Empty); }
public void RouteDataActionConstraint_RequireKeyByPassingNonEmpty() { var routeDataConstraint = new RouteDataActionConstraint("key", "value"); Assert.Equal(routeDataConstraint.RouteKey, "key"); Assert.Equal(routeDataConstraint.KeyHandling, RouteKeyHandling.RequireKey); Assert.Equal(routeDataConstraint.RouteValue, "value"); }
public void RouteDataActionConstraint_RequireKeyByPassingNonEmpty() { var routeDataConstraint = new RouteDataActionConstraint("key", "value"); Assert.Equal(routeDataConstraint.RouteKey, "key"); Assert.Equal(routeDataConstraint.KeyHandling, RouteKeyHandling.RequireKey); Assert.Equal(routeDataConstraint.RouteValue, "value"); }
public void RouteDataActionConstraint_DenyKeyByPassingNull() { var routeDataConstraint = new RouteDataActionConstraint("key", null); Assert.Equal(routeDataConstraint.RouteKey, "key"); Assert.Equal(routeDataConstraint.KeyHandling, RouteKeyHandling.DenyKey); Assert.Equal(routeDataConstraint.RouteValue, string.Empty); }
private IEnumerable <ActionDescriptor> GetActions(Type type) { var prototype = (MvcModule)ActivatorUtilities.CreateInstance(_services, type); int i = 0; foreach (var action in prototype.Actions) { var filters = type.GetTypeInfo() .GetCustomAttributes(inherit: true) .OfType <IFilterMetadata>() .Select(filter => new FilterDescriptor(filter, FilterScope.Controller)) .OrderBy(d => d, FilterDescriptorOrderComparer.Comparer) .ToList(); RouteDataActionConstraint pathConstraint; if (action.Path == "/") { pathConstraint = new RouteDataActionConstraint("modulepath", string.Empty); } else { pathConstraint = new RouteDataActionConstraint("modulepath", action.Path.Substring(1)); } yield return(new ModuleActionDescriptor() { FilterDescriptors = filters, Index = i++, ActionConstraints = new List <IActionConstraintMetadata>() { new HttpMethodActionConstraint(new [] { action.Verb }), }, ModuleType = type, Parameters = new List <ParameterDescriptor>(), // No Parameter support in this sample RouteConstraints = new List <RouteDataActionConstraint>() { new RouteDataActionConstraint("module", "true"), // only match a 'module' route pathConstraint, } }); } }
private IEnumerable<ActionDescriptor> GetActions(Type type) { var prototype = (MvcModule)_typeActivator.CreateInstance(_services, type); int i = 0; foreach (var action in prototype.Actions) { var filters = type.GetTypeInfo() .GetCustomAttributes(inherit: true) .OfType<IFilter>() .Select(filter => new FilterDescriptor(filter, FilterScope.Controller)) .OrderBy(d => d, FilterDescriptorOrderComparer.Comparer) .ToList(); RouteDataActionConstraint pathConstraint; if (action.Path == "/") { pathConstraint = new RouteDataActionConstraint("modulepath", RouteKeyHandling.DenyKey); } else { pathConstraint = new RouteDataActionConstraint("modulepath", action.Path.Substring(1)); } yield return new ModuleActionDescriptor() { FilterDescriptors = filters, Index = i++, MethodConstraints = new List<HttpMethodConstraint>() { new HttpMethodConstraint(new [] { action.Verb }), }, ModuleType = type, Parameters = new List<ParameterDescriptor>(), // No Parameter support in this sample RouteConstraints = new List<RouteDataActionConstraint>() { new RouteDataActionConstraint("module", "true"), // only match a 'module' route pathConstraint, } }; } }
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)); } }
public RouteDataActionConstraintValues([NotNull] RouteDataActionConstraint inner) { RouteKey = inner.RouteKey; RouteValue = inner.RouteValue; KeyHandling = inner.KeyHandling; }