我正在尝试在我的 asp.net mvc 项目中路由 .aspx (Webforms 页面)。我在 global.asax 中注册该页面:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("Tickets", "Reports/Tickets", "~/WebForms/Reports/Tickets.aspx");
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
问题是,在我添加第二行后,该站点停止进入我的主控制器(索引操作)并重定向到:http://localhost:37538/Reports/Tickets?action=Index&controller=Login%22
总是我运行这个项目。
项目详情:
- Asp.Net MVC 3
- 表单验证
- .Net 4.0
Obs:要重现此错误,请创建一个新的 asp.net mvc 项目作为互联网应用程序,然后创建Tickets
网页表单页面内/WebForms/Reports
文件夹,并注册新路线。运行该项目(可能您已登录),因此现在注销,您将被重定向到http://localhost:35874/Reports/Tickets?action=LogOff&controller=Account
, 所以为什么?
Solved!因此,我们需要向 Webforms 路由添加路由约束,以确保它仅捕获传入路由,而不捕获传出路由生成。
将以下类添加到您的项目中(在新文件中或在 global.asax.cs 的底部):
public class MyCustomConstraint : 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 MyCustomConstraint() } }
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)