public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // Redirect From Old Route to New route var targetRoute = routes.Map("target", "yo/{id}/{action}", new { controller = "Home" }); routes.Redirect(r => r.MapRoute("legacy", "foo/{id}/baz/{action}")).To(targetRoute, new { id = "123", action = "index" }); routes.Redirect(r => r.MapRoute("legacy2", "foo/baz")).To(targetRoute, new { id = "123", action = "index" }); // Map Delegate routes.MapDelegate("map-delegate", "this-is-a-test", rc => rc.HttpContext.Response.Write("Yeah, it's a test")); routes.MapDelegate("map-delegate-incoming-only", "this-is-a-test", new { whatever = new IncomingOnlyRouteConstraint() }, rc => rc.HttpContext.Response.Write("Yeah, it's a test")); // Map HTTP Handlers routes.MapHttpHandler<HelloWorldHttpHandler>("hello-world", "handlers/helloworld"); routes.MapHttpHandler("hello-world2", "handlers/helloworld2", new HelloWorldHttpHandler()); RouteCollection someRoutes = new RouteCollection(); someRoutes.MapHttpHandler<HelloWorldHttpHandler>("hello-world3", "handlers/helloworld3"); someRoutes.MapHttpHandler("hello-world4", "handlers/helloworld4", new HelloWorldHttpHandler()); var groupRoute = new GroupRoute("~/section", someRoutes); routes.Add("group", groupRoute); var mvcRoutes = new RouteCollection(); mvcRoutes.Map("foo1", "foo/{controller}", new { action = "index" }); mvcRoutes.Map("foo2", "foo2/{controller}", new { action = "index" }); routes.Add("group2", new GroupRoute("~/group2sec", mvcRoutes)); var defaultRoute = routes.Map( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ).SetRouteName("Default"); }
public void GetRouteDataMatchesChildRouteRequestWithParentAtRoot() { // Arrange var httpContext = new Mock<HttpContextBase>(); httpContext.Setup(c => c.Request.Path).Returns("/foo/bar"); httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/foo/bar"); var routeHandler = new Mock<IRouteHandler>().Object; var childRoutes = new RouteCollection(); childRoutes.Add(new Route("no-match", new Mock<IRouteHandler>().Object)); childRoutes.Add(new Route("foo/bar", routeHandler)); var virtualPathResolver = new Mock<IVirtualPathResolver>(); virtualPathResolver.Setup(r => r.ToAbsolute("~/")).Returns("/"); var groupRoute = new GroupRoute(childRoutes, virtualPathResolver.Object); // Act var routeData = groupRoute.GetRouteData(httpContext.Object); // Assert PAssert.IsTrue(() => routeData != null); PAssert.IsTrue(() => routeHandler == routeData.RouteHandler); }
public void GetRouteDataGetsMatchingChildRouteData() { // Arrange var httpContext = new Mock<HttpContextBase>(); httpContext.Setup(c => c.Request.Path).Returns("/foo/bar/uno/dos/tres"); httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/foo/bar/uno/dos/tres"); var routeHandler = new Mock<IRouteHandler>().Object; var childRoutes = new RouteCollection(); childRoutes.Add(new Route("no-match", new Mock<IRouteHandler>().Object)); childRoutes.Add(new Route("uno/{seg}/{seg2}", routeHandler)); var virtualPathResolver = new Mock<IVirtualPathResolver>(); virtualPathResolver.Setup(r => r.ToAbsolute("~/foo/bar")).Returns("/foo/bar"); var groupRoute = new GroupRoute("~/foo/bar", childRoutes, virtualPathResolver.Object); // Act var routeData = groupRoute.GetRouteData(httpContext.Object); // Assert PAssert.IsTrue(() => routeData != null); PAssert.IsTrue(() => routeData.Values["seg"].Equals("dos")); PAssert.IsTrue(() => routeData.Values["seg2"].Equals("tres")); }
public void GetVirtualPathWithNameSuppliedReturnsPathToChildRoute() { // Arrange var childRoutes = new RouteCollection(); childRoutes.Add("r1", new Route("no-match", new Mock<IRouteHandler>().Object)); childRoutes.Add("r2", new Route("uno/{seg}/{seg2}", new Mock<IRouteHandler>().Object)); var virtualPathResolver = new Mock<IVirtualPathResolver>(); virtualPathResolver.Setup(r => r.ToAbsolute("~/foo/bar")).Returns("/foo/bar"); var groupRoute = new GroupRoute("~/foo/bar", childRoutes, virtualPathResolver.Object); var httpContext = new Mock<HttpContextBase>(); httpContext.Setup(c => c.Request.Path).Returns("/qux"); httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/qux"); httpContext.Setup(c => c.Request.ApplicationPath).Returns("/"); httpContext.Setup(c => c.Response.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(s => s); var requestContext = new RequestContext(httpContext.Object, new RouteData()); // Act var routeValues = new RouteValueDictionary { { "seg", "dos" }, { "seg2", "three" } }.SetRouteName("r2"); var virtualPath = groupRoute.GetVirtualPath(requestContext, routeValues); // Assert PAssert.IsTrue(() => virtualPath != null); PAssert.IsTrue(() => virtualPath.VirtualPath == "foo/bar/uno/dos/three"); }