我正在尝试在我的 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 });
问题是,在我添加第二行之后,站点停止进入我的家庭 Controller (索引操作)并重定向到:http://localhost:37538/Reports/Tickets?action=Index&controller =Login%22
始终是我运行的项目。
项目详情:
- ASP.Net MVC 3
- 表单验证
- .Net 4.0
Obs:要重现此错误,创建一个新的 asp.net mvc 项目作为 Internet 应用程序,在 /WebForms/Reports
文件夹中创建 Tickets
webforms 页面后,并注册新路线。运行项目(可能您已登录),所以现在注销,您将被重定向到 http://localhost:35874/Reports/Tickets?action=LogOff&controller=Account
,为什么?
最佳答案
已解决!因此,我们需要向 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() } }
);
关于c# - asp.net mvc项目添加MapPageRoute后站点停止进入Home Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10178276/