Example #1
0
        public ActionDescriptorCreator FindAction(ControllerContext controllerContext, string actionName)
        {
            if (controllerContext == null)
            {
                throw Error.ArgumentNull("controllerContext");
            }

            if (controllerContext.RouteData != null)
            {
                MethodInfo target = controllerContext.RouteData.GetTargetActionMethod();
                if (target != null)
                {
                    // short circuit the selection process if a direct route was matched.
                    return(GetActionDescriptorDelegate(target));
                }
            }

            List <MethodInfo> finalMethods = ActionMethodSelector.FindActionMethods(controllerContext, actionName, AliasedMethods, NonAliasedMethods);

            switch (finalMethods.Count)
            {
            case 0:
                return(null);

            case 1:
                MethodInfo entryMethod = finalMethods[0];
                return(GetActionDescriptorDelegate(entryMethod));

            default:
                throw CreateAmbiguousActionMatchException(finalMethods, actionName);
            }
        }
Example #2
0
        /// <summary>
        /// Convenience method used to generate a link
        /// using Routing to determine the virtual path.
        /// </summary>
        /// <param name="routeKey">The name of the route, if any.</param>
        /// <param name="action">The action with parameters.</param>
        /// <typeparam name="TController">The type of the controller.</typeparam>
        public virtual string Route <TController>(string routeKey,
                                                  Expression <Action <TController> > action)
            where TController : IController
        {
            Precondition.Require(action, () => Error.ArgumentNull("action"));

            MethodCallExpression mexp = (action.Body as MethodCallExpression);

            if (mexp == null)
            {
                throw Error.ExpressionMustBeAMethodCall("action");
            }

            if (mexp.Object != action.Parameters[0])
            {
                throw Error.MethodCallMustTargetLambdaArgument("action");
            }

            string actionName     = ActionMethodSelector.GetNameOrAlias(mexp.Method);
            string controllerName = typeof(TController).Name;

            ValueDictionary rvd = LinqHelper.ExtractArgumentsToDictionary(mexp);

            rvd = (rvd != null) ? rvd : new ValueDictionary();

            return(HttpUtility.HtmlAttributeEncode(GenerateUrl(routeKey, controllerName, actionName, rvd)));
        }
        public void ControllerTypeProperty()
        {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act & Assert
            Assert.Same(controllerType, selector.ControllerType);
        }
        public void ControllerTypeProperty()
        {
            // Arrange
            Type controllerType           = typeof(MethodLocatorController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act & Assert
            Assert.Same(controllerType, selector.ControllerType);
        }
        public void FindActionMethodReturnsMethodMultipleMatchesOverOneOptOut()
        {
            // Arrange
            Type controllerType           = typeof(ControllerMatchMultipleAttributes);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(new ControllerContext(), "Match");

            // Assert
            Assert.Equal("Match", matchedMethod.Name);
        }
        public void FindActionMethodReturnsNullIfNoMethodMatches()
        {
            // Arrange
            Type controllerType           = typeof(SelectionAttributeController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(null, "ZeroMatch");

            // Assert
            Assert.Null(matchedMethod);
        }
Example #7
0
        public void FindActionMethodReturnsMethodWithAttributeWhenOthersOptOutAndNoAttribute()
        {
            // Arrange
            Type controllerType           = typeof(ControllerMatchMiddleWithAttribute);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(new ControllerContext(), "MiddleMatch");

            // Assert
            Assert.Equal("MiddleMatch", matchedMethod.Name);
        }
        public void FindActionMethod_IgnoresDirectRouteAttributedMethods_NoMatch()
        {
            // Arrange
            Type controllerType           = typeof(WithRoutingAttributeController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(new ControllerContext(), "DirectRouteOnly");

            // Assert
            Assert.Null(matchedMethod);
        }
        public void FindActionMethodReturnsNullIfNoMethodMatches()
        {
            // Arrange
            Type controllerType = typeof(SelectionAttributeController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(null, "ZeroMatch");

            // Assert
            Assert.Null(matchedMethod);
        }
        public void FindActionMethodReturnsMethodWithoutAttributeWhenOthersOptOut()
        {
            // Arrange
            Type controllerType = typeof(ControllerMatchMiddleNoAttribute);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(null, "MiddleMatch");

            // Assert
            Assert.Equal("MiddleMatch", matchedMethod.Name);
        }
        public void FindActionMethodReturnsMatchingMethodIfOneMethodMatches()
        {
            // Arrange
            Type controllerType           = typeof(SelectionAttributeController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(null, "OneMatch");

            // Assert
            Assert.Equal("OneMatch", matchedMethod.Name);
            Assert.Equal(typeof(string), matchedMethod.GetParameters()[0].ParameterType);
        }
        public void FindActionMethodThrowsIfMultipleMethodsMatch()
        {
            // Arrange
            Type controllerType           = typeof(SelectionAttributeController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act & veriy
            Assert.Throws <AmbiguousMatchException>(
                delegate { selector.FindActionMethod(null, "TwoMatch"); },
                @"The current request for action 'TwoMatch' on controller type 'SelectionAttributeController' is ambiguous between the following action methods:
Void TwoMatch2() on type System.Web.Mvc.Test.ActionMethodSelectorTest+SelectionAttributeController
Void TwoMatch() on type System.Web.Mvc.Test.ActionMethodSelectorTest+SelectionAttributeController");
        }
        public void FindActionMethodReturnsMatchingMethodIfOneMethodMatches()
        {
            // Arrange
            Type controllerType = typeof(SelectionAttributeController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(null, "OneMatch");

            // Assert
            Assert.Equal("OneMatch", matchedMethod.Name);
            Assert.Equal(typeof(string), matchedMethod.GetParameters()[0].ParameterType);
        }
        public void GetSelector()
        {
            // Arrange
            Type controllerType             = typeof(object);
            ActionMethodSelectorCache cache = new ActionMethodSelectorCache();

            // Act
            ActionMethodSelector selector1 = cache.GetSelector(controllerType);
            ActionMethodSelector selector2 = cache.GetSelector(controllerType);

            // Assert
            Assert.AreSame(controllerType, selector1.ControllerType);
            Assert.AreSame(selector1, selector2, "Selector was not correctly cached.");
        }
        public void AliasedMethodsProperty()
        {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);

            // Act
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Assert
            Assert.Equal(2, selector.AliasedMethods.Length);

            List<MethodInfo> sortedAliasedMethods = selector.AliasedMethods.OrderBy(methodInfo => methodInfo.Name).ToList();
            Assert.Equal("Bar", sortedAliasedMethods[0].Name);
            Assert.Equal("FooRenamed", sortedAliasedMethods[1].Name);
        }
        public void FindActionMethodReturnsMethodWithActionSelectionAttributeIfMultipleMethodsMatchRequest()
        {
            // DevDiv Bugs 212062: If multiple action methods match a request, we should match only the methods with an
            // [ActionMethod] attribute since we assume those methods are more specific.

            // Arrange
            Type controllerType = typeof(SelectionAttributeController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(null, "ShouldMatchMethodWithSelectionAttribute");

            // Assert
            Assert.Equal("MethodHasSelectionAttribute1", matchedMethod.Name);
        }
        public void FindActionMethodReturnsMethodWithActionSelectionAttributeIfMultipleMethodsMatchRequest()
        {
            // DevDiv Bugs 212062: If multiple action methods match a request, we should match only the methods with an
            // [ActionMethod] attribute since we assume those methods are more specific.

            // Arrange
            Type controllerType           = typeof(SelectionAttributeController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(null, "ShouldMatchMethodWithSelectionAttribute");

            // Assert
            Assert.Equal("MethodHasSelectionAttribute1", matchedMethod.Name);
        }
        public void FindActionMethod_IgnoresDirectRouteAttributedMethods_NoMatch()
        {
            // Arrange
            Type controllerType           = typeof(WithRoutingAttributeController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // This simulates what AttributeRoutingMapper will do
            selector.StandardRouteMethods.Remove(controllerType.GetMethod("ActionWithoutRoute"));
            selector.StandardRouteMethods.Remove(controllerType.GetMethod("DirectRouteOnly"));

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(new ControllerContext(), "DirectRouteOnly");

            // Assert
            Assert.Null(matchedMethod);
        }
        public void AliasedMethodsProperty()
        {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);

            // Act
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Assert
            Assert.Equal(2, selector.AliasedMethods.Length);

            List <MethodInfo> sortedAliasedMethods = selector.AliasedMethods.OrderBy(methodInfo => methodInfo.Name).ToList();

            Assert.Equal("Bar", sortedAliasedMethods[0].Name);
            Assert.Equal("FooRenamed", sortedAliasedMethods[1].Name);
        }
Example #20
0
        public void FindActionMethod_MultipleMethodsSameActionOneWithRouteAttributeAndRouteWasMatched_ReturnsMethodWithRoutingAttribute()
        {
            // Arrange
            Type controllerType           = typeof(WithRoutingAttributeController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            var context = new ControllerContext();

            context.RouteData       = new RouteData();
            context.RouteData.Route = DirectRouteTestHelpers.BuildDirectRouteStubsFrom <WithRoutingAttributeController>(c => c.Action())[0];

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(context, "Action");

            // Assert
            Assert.Equal("Action", matchedMethod.Name);
        }
        public ActionDescriptorCreator FindAction(ControllerContext controllerContext, string actionName)
        {
            List <MethodInfo> finalMethods = ActionMethodSelector.FindActionMethods(controllerContext, actionName, AliasedMethods, NonAliasedMethods);

            switch (finalMethods.Count)
            {
            case 0:
                return(null);

            case 1:
                MethodInfo entryMethod = finalMethods[0];
                return(GetActionDescriptorDelegate(entryMethod));

            default:
                throw CreateAmbiguousActionMatchException(finalMethods, actionName);
            }
        }
        public void NonAliasedMethodsProperty()
        {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);

            // Act
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Assert
            Assert.Single(selector.NonAliasedMethods);

            List <MethodInfo> sortedMethods = selector.NonAliasedMethods["foo"].OrderBy(methodInfo => methodInfo.GetParameters().Length).ToList();

            Assert.Equal("Foo", sortedMethods[0].Name);
            Assert.Empty(sortedMethods[0].GetParameters());
            Assert.Equal("Foo", sortedMethods[1].Name);
            Assert.Equal(typeof(string), sortedMethods[1].GetParameters()[0].ParameterType);
        }
        public static Route MapAsyncRoute <TController>(this RouteCollection routes,
                                                        string key, string url, Expression <Action <TController> > action,
                                                        params IRouteConstraint[] constraints)
            where TController : IAsyncController
        {
            Precondition.Require(routes, () => Error.ArgumentNull("routes"));
            Precondition.Require(url, () => Error.ArgumentNull("url"));

            MethodCallExpression mexp = (action.Body as MethodCallExpression);

            if (mexp == null)
            {
                throw Error.ExpressionMustBeAMethodCall("action");
            }

            if (mexp.Object != action.Parameters[0])
            {
                throw Error.MethodCallMustTargetLambdaArgument("action");
            }

            string actionName     = ActionMethodSelector.GetNameOrAlias(mexp.Method);
            string controllerName = typeof(TController).Name;

            ValueDictionary defaults = LinqHelper.ExtractArgumentsToDictionary(mexp);

            if (defaults == null)
            {
                defaults = new ValueDictionary();
            }

            defaults["controller"] = controllerName;
            defaults["action"]     = actionName;

            Route route = (constraints == null) ?
                          new Route(url, defaults, constraints, new MvcAsyncRouteHandler()) :
                          new Route(url, defaults, new MvcAsyncRouteHandler());

            routes.Add(key, route);
            return(route);
        }
        public void FindActionMethodThrowsIfMultipleMethodsMatch()
        {
            // Arrange
            Type controllerType = typeof(SelectionAttributeController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act & veriy
            Assert.Throws<AmbiguousMatchException>(
                delegate { selector.FindActionMethod(null, "TwoMatch"); },
                @"The current request for action 'TwoMatch' on controller type 'SelectionAttributeController' is ambiguous between the following action methods:
Void TwoMatch2() on type System.Web.Mvc.Test.ActionMethodSelectorTest+SelectionAttributeController
Void TwoMatch() on type System.Web.Mvc.Test.ActionMethodSelectorTest+SelectionAttributeController");
        }
        public void FindActionMethod_IgnoresDirectRouteAttributedMethods_NoMatch()
        {
            // Arrange
            Type controllerType = typeof(WithRoutingAttributeController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // This simulates what AttributeRoutingMapper will do
            selector.StandardRouteMethods.Remove(controllerType.GetMethod("ActionWithoutRoute"));
            selector.StandardRouteMethods.Remove(controllerType.GetMethod("DirectRouteOnly"));

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(new ControllerContext(), "DirectRouteOnly");

            // Assert
            Assert.Null(matchedMethod);
        }
        public void FindActionMethod_IgnoresDirectRouteAttributedMethods_NoMatch()
        {
            // Arrange
            Type controllerType = typeof(WithRoutingAttributeController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(new ControllerContext(), "DirectRouteOnly");

            // Assert
            Assert.Null(matchedMethod);
        }
        public void NonAliasedMethodsProperty()
        {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);

            // Act
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Assert
            Assert.Single(selector.NonAliasedMethods);

            List<MethodInfo> sortedMethods = selector.NonAliasedMethods["foo"].OrderBy(methodInfo => methodInfo.GetParameters().Length).ToList();
            Assert.Equal("Foo", sortedMethods[0].Name);
            Assert.Empty(sortedMethods[0].GetParameters());
            Assert.Equal("Foo", sortedMethods[1].Name);
            Assert.Equal(typeof(string), sortedMethods[1].GetParameters()[0].ParameterType);
        }
        public void FindActionMethod_MultipleMethodsSameActionOneWithRouteAttributeAndRouteWasMatched_ReturnsMethodWithRoutingAttribute()
        {
            // Arrange
            Type controllerType = typeof(WithRoutingAttributeController);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            var context = new ControllerContext();
            context.RouteData = new RouteData();
            context.RouteData.Route = DirectRouteTestHelpers.BuildDirectRouteStubsFrom<WithRoutingAttributeController>(c => c.Action())[0];

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(context, "Action");

            // Assert
            Assert.Equal("Action", matchedMethod.Name);
        }
        public void FindActionMethodReturnsMethodMultipleMatchesOverOneOptOut()
        {
            // Arrange
            Type controllerType = typeof(ControllerMatchMultipleAttributes);
            ActionMethodSelector selector = new ActionMethodSelector(controllerType);

            // Act
            MethodInfo matchedMethod = selector.FindActionMethod(null, "Match");

            // Assert
            Assert.Equal("Match", matchedMethod.Name);
        }