예제 #1
0
        public async Task AttributeRouting_WithControllerActionDescriptor()
        {
            // Arrange
            var controllerType = typeof(HomeController);
            var actionMethod   = controllerType.GetMethod("Index");

            var action = new ControllerActionDescriptor();

            action.DisplayName      = "Microsoft.AspNetCore.Mvc.Routing.AttributeRoutingTest+HomeController.Index";
            action.MethodInfo       = actionMethod;
            action.RouteConstraints = new List <RouteDataActionConstraint>()
            {
                new RouteDataActionConstraint(TreeRouter.RouteGroupKey, "group"),
            };
            action.AttributeRouteInfo          = new AttributeRouteInfo();
            action.AttributeRouteInfo.Template = "{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.AspNetCore.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 handler  = CreateRouter();
            var services = CreateServices(action);

            var route = AttributeRouting.CreateAttributeMegaRoute(handler, services);

            // Act & Assert
            var ex = await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                await route.RouteAsync(new RouteContext(new DefaultHttpContext()));
            });

            Assert.Equal(expectedMessage, ex.Message);
        }
예제 #2
0
        public async Task AttributeRouting_WithControllerActionDescriptor()
        {
            // Arrange
            var controllerType = typeof(HomeController);
            var actionMethod   = controllerType.GetMethod("Index");

            var action = new ControllerActionDescriptor();

            action.DisplayName = "Microsoft.AspNetCore.Mvc.Routing.AttributeRoutingTest+HomeController.Index";
            action.MethodInfo  = actionMethod;
            action.RouteValues = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "controller", "Home" },
                { "action", "Index" },
            };
            action.AttributeRouteInfo          = new AttributeRouteInfo();
            action.AttributeRouteInfo.Template = "{controller}/{action}";

            var expectedMessage =
                "The following errors occurred with attribute routing information:" + Environment.NewLine +
                Environment.NewLine +
                "For action: 'Microsoft.AspNetCore.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 services = CreateServices(action);

            var route = AttributeRouting.CreateAttributeMegaRoute(services);

            // Act & Assert
            var ex = await Assert.ThrowsAsync <RouteCreationException>(async() =>
            {
                await route.RouteAsync(new RouteContext(new DefaultHttpContext()));
            });

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