将MapPageRoute添加到asp.net mvc项目后,该站点将停止进入Home Controller

Vin*_*oni 5 c# asp.net-mvc routing webforms

我正在尝试在我的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 });
Run Code Online (Sandbox Code Playgroud)

问题是,在我添加第二行之后,站点停止进入我的Home Controller(索引操作)并重定向到:http://localhost:37538/Reports/Tickets?action=Index&controller=Login%22始终我运行项目.

项目详情:

  • Asp.Net MVC 3
  • 表单身份验证
  • .Net 4.0

Obs:要重现此错误,请在创建文件夹中的Ticketswebforms页面后,创建一个新的asp.net mvc项目作为Internet应用程序/WebForms/Reports,并注册新路由.运行项目(可能你已经记录),所以现在注销,你将被重定向到http://localhost:35874/Reports/Tickets?action=LogOff&controller=Account,为什么呢?

Vin*_*oni 11

解决了!因此,我们需要在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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后将故障单路由更改为以下内容:

routes.MapPageRoute(
    "Tickets",
    "Reports/Tickets",
    "~/WebForms/Reports/Tickets.aspx",
    true, null, 
    new RouteValueDictionary { { "outgoing", new MyCustomConstraint() } }
);
Run Code Online (Sandbox Code Playgroud)