如何在asp.net mvc 3项目中路由.aspx页面?
我在以下路径中有一个.aspx页面:
Areas/Management/Views/Ticket/Report.aspx
我想在浏览器中将其路由到以下路径:
http://localhost/Reports/Tickets
我怎样才能做到这一点?
我试试这个:
routes.MapRoute( "Tickets", // Route name "Areas/Management/Views/Ticket/Report.aspx", // Original URL new { controller = "Reports", action = "Tickets" } // New URL );
但我得到了404
错误。
我做错了什么?
Obs:我把它放在Default
路线之前。
如果您正在尝试在MVC项目中使用Web表单,那么我会将.aspx移出views文件夹,因为它实际上不是一个视图,所以像WebForms / Tickets / Report.aspx。
在Web表单中,您通过调用MapPageRoute
方法映射路由。
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapPageRoute("Tickets", "Reports/Tickets", "~/WebForms/Tickets/Report.aspx"); routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
您需要将其放在默认的MVC路由之前。
解决了! 因此,我们需要在webforms路由中添加路由约束,以确保它只捕获传入路由,而不是传出路由生成。
将以下类添加到项目中(在新文件或global.asax.cs的底部):
public class MyCustomConstaint : IRouteConstraint{ public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection){ return routeDirection == RouteDirection.IncomingRequest; } }
然后将Tickets路由更改为以下内容:
routes.MapPageRoute( "Tickets", "Reports/Tickets", "~/WebForms/Reports/Tickets.aspx", true, null, new RouteValueDictionary { { "outgoing", new MyCustomConstaint() } } );
你正在做相反的事情。 这会将您的urlAreas/Management/Views/Ticket/Report.aspx
到{ controller = "Reports", action = "Tickets" }
你应该做的是将url设置为
Reports/Tickets
编辑: – 您可以创建一个routeHandler,仅用于路由到此.aspx页面..就像这样。
public class ASPXRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { return BuildManager.CreateInstanceFromVirtualPath("~/Areas/Management/Views/Ticket/Report.aspx", typeof(Page)) as Page; } }
那么你可以使用你的ur路由到现有的路由表
Route customRoute = new Route("Reports/Ticket",null, new ASPXRouteHandler()); routes.Add(customRoute);
如果在创建asp.net项目时保留默认路由
public class ReportsController : Controller { public ActionResult Ticket() { return View(); } }
这应该可以解决问题。 asp.net mvc中的路由意味着您不直接链接到.aspx,而是链接到Actions(方法),而Actions又返回适当的视图( .aspx)
- 网络使用HttpWebRequest进行身份validation和网站身份validation
- 在回发时维护滚动位置和选定控件
- ASP.NET web api无法获得application / x-www-form-urlencoded HTTP POST
- 从ASP.NET启动流程 – 为什么流程会立即死亡?
- 每个请求都有Azure 400 Bad Request
- DirectoryServices UserPrincipal.SetPassword忽略密码策略(密码历史记录)
- WebOperationContext.current和HttpContext.Current之间的区别
- 如何使用c#应用程序上传和下载文件到onedrive for business?
- 是否可以在MVC 3中打开/关闭validation数据注释?