コード例 #1
0
        public void AttributeRouting_SyntaxErrorInTemplate()
        {
            // Arrange
            var action = CreateAction("InvalidTemplate", "{a/dkfk}");

            var expectedMessage =
                "The following errors occurred with attribute routing information:" + Environment.NewLine +
                Environment.NewLine +
                "For action: 'InvalidTemplate'" + Environment.NewLine +
                "Error: The route parameter name 'a/dkfk' is invalid. Route parameter names must be non-empty and " +
                "cannot contain these characters: '{', '}', '/'. The '?' character marks a parameter as optional, " +
                "and can occur only at the end of the parameter. The '*' character marks a parameter as catch-all, " +
                "and can occur only at the start of the parameter." + Environment.NewLine +
                "Parameter name: routeTemplate";

            var handler  = CreateRouter();
            var services = CreateServices(action);

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(() =>
            {
                AttributeRouting.CreateAttributeMegaRoute(handler, services);
            });

            Assert.Equal(expectedMessage, ex.Message);
        }
コード例 #2
0
        public void AttributeRouting_MultipleErrors()
        {
            // Arrange
            var action1 = CreateAction("DisallowedParameter1", "{foo}/{action}");

            action1.RouteValueDefaults.Add("foo", "bleh");

            var action2 = CreateAction("DisallowedParameter2", "cool/{action}");

            action2.RouteValueDefaults.Add("action", "hey");

            var expectedMessage =
                "The following errors occurred with attribute routing information:" + Environment.NewLine +
                Environment.NewLine +
                "For action: 'DisallowedParameter1'" + Environment.NewLine +
                "Error: The attribute route '{foo}/{action}' cannot contain a parameter named '{foo}'. " +
                "Use '[foo]' in the route template to insert the value 'bleh'." + Environment.NewLine +
                Environment.NewLine +
                "For action: 'DisallowedParameter2'" + Environment.NewLine +
                "Error: The attribute route 'cool/{action}' cannot contain a parameter named '{action}'. " +
                "Use '[action]' in the route template to insert the value 'hey'.";

            var handler  = CreateRouter();
            var services = CreateServices(action1, action2);

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(() =>
            {
                AttributeRouting.CreateAttributeMegaRoute(handler, services);
            });

            Assert.Equal(expectedMessage, ex.Message);
        }
コード例 #3
0
ファイル: AttributeRoutingTest.cs プロジェクト: gmmateev/Mvc
        public void AttributeRouting_WithReflectedActionDescriptor()
        {
            // Arrange
            var controllerType = typeof(HomeController);
            var actionMethod   = controllerType.GetMethod("Index");

            var action = new ReflectedActionDescriptor();

            action.DisplayName      = "Microsoft.AspNet.Mvc.Routing.AttributeRoutingTest+HomeController.Index";
            action.MethodInfo       = actionMethod;
            action.RouteConstraints = new List <RouteDataActionConstraint>()
            {
                new RouteDataActionConstraint(AttributeRouting.RouteGroupKey, "group"),
            };
            action.AttributeRouteTemplate = "{controller}/{action}";
            action.RouteValueDefaults.Add("controller", "Home");
            action.RouteValueDefaults.Add("action", "Index");

            var expectedMessage =
                "The following errors occurred with attribute routing information:" + Environment.NewLine +
                Environment.NewLine +
                "For action: 'Microsoft.AspNet.Mvc.Routing.AttributeRoutingTest+HomeController.Index'" + Environment.NewLine +
                "Error: The attribute route '{controller}/{action}' cannot contain a parameter named '{controller}'. " +
                "Use '[controller]' in the route template to insert the value 'Home'.";

            var router   = CreateRouter();
            var services = CreateServices(action);

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(
                () => { AttributeRouting.CreateAttributeMegaRoute(router, services); });

            Assert.Equal(expectedMessage, ex.Message);
        }
コード例 #4
0
        public void AttributeRouting_SyntaxErrorInTemplate()
        {
            // Arrange
            var action = CreateAction("InvalidTemplate", "{a/dkfk}");

            var expectedMessage =
                "The following errors occurred with attribute routing information:" + Environment.NewLine +
                Environment.NewLine +
                "For action: 'InvalidTemplate'" + Environment.NewLine +
                "Error: There is an incomplete parameter in the route template. " +
                "Check that each '{' character has a matching '}' character." + Environment.NewLine +
                "Parameter name: routeTemplate";

            var router   = CreateRouter();
            var services = CreateServices(action);

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(
                () => { AttributeRouting.CreateAttributeMegaRoute(router, services); });

            Assert.Equal(expectedMessage, ex.Message);
        }