示例#1
0
        public static RouteData FindRouteDataByUrl(this RouteCollection routes, HttpContext httpContext, string url)
        {
            if (routes == null)
            {
                throw new ArgumentNullException("routes");
            }
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }
            var injectedHttpContext = new HttpContextInjector(httpContext, new HttpRequestInjector(httpContext.Request, url));

            return(routes.GetRouteData(injectedHttpContext));
        }
示例#2
0
        public void AttributeRouting_WithInheritance_InvalidPaths(Type derivedController, string path)
        {
            // Arrange
            var controllerTypes = new[] { derivedController, derivedController.BaseType };
            var routes          = new RouteCollection();

            routes.MapMvcAttributeRoutes(controllerTypes);
            HttpContextBase context = GetContext(path);

            // Act
            RouteData routeData = routes.GetRouteData(context);

            // Assert
            Assert.Null(routeData);
        }
示例#3
0
        public void AttributeRouting_AmbiguousActions_ThrowsAmbiguousException(Type controllerType, string path)
        {
            // Arrange
            var controllerTypes = new[] { controllerType };
            var routes          = new RouteCollection();

            routes.MapMvcAttributeRoutes(controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            Assert.Throws <AmbiguousMatchException>(() => handler.ProcessRequest(context));
        }
示例#4
0
        public void AttributeRouting_AmbiguousActions_ThrowsAmbiguousException(Type controllerType, string path)
        {
            // Arrange
            var controllerTypes = new[] { controllerType };
            var routes          = new RouteCollection();

            routes.MapMvcAttributeRoutes(controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            // Act
            // Bug 1285: Attribute routing with ambiguous actions should throw an AmbiguousMatchException.
            // This test should fail once that is fixed. Uncomment the line below then.
            // Assert.Throws<AmbiguousMatchException>(() => handler.ProcessRequest(context));
            Assert.DoesNotThrow(() => handler.ProcessRequest(context));
        }
        public void AttributeRouting_MixedWithGeneralRouting(
            Type controllerType,
            string path,
            string expectedAction
            )
        {
            // Arrange
            var    controllerTypes = new[] { controllerType };
            var    routes          = new RouteCollection();
            object defaults        = new
            {
                controller = controllerType.Name.Substring(0, controllerType.Name.Length - 10)
            };

            routes.Add(new Route("standard/{action}", new RouteValueDictionary(defaults), null));
            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            if (expectedAction == null)
            {
                // Act & Assert
                Assert.Throws <HttpException>(() => handler.ProcessRequest(context));
            }
            else
            {
                // Act
                handler.ProcessRequest(context);

                // Assert
                ContentResult result = Assert.IsType <ContentResult>(context.Items[ResultKey]);
                Assert.Equal(expectedAction, result.Content);
            }
        }
示例#6
0
        public void AttributeRouting_WithInheritance_MethodOverrides(Type derivedController, string path, string expectedAction)
        {
            // Arrange
            var controllerTypes = new[] { derivedController, derivedController.BaseType };
            var routes          = new RouteCollection();

            routes.MapMvcAttributeRoutes(controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            // Act
            handler.ProcessRequest(context);

            // Assert
            ContentResult result = Assert.IsType <ContentResult>(context.Items[ResultKey]);

            Assert.Equal(expectedAction, result.Content);
        }
示例#7
0
        public void AttributeRouting_MethodOverloads_WithDifferentActionNames(Type controllerType, string path, string expectedAction)
        {
            // Arrange
            var controllerTypes = new[] { controllerType };
            var routes          = new RouteCollection();

            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            // Act
            handler.ProcessRequest(context);

            // Assert
            ContentResult result = Assert.IsType <ContentResult>(context.Items[ResultKey]);

            Assert.Equal(expectedAction, result.Content);
        }