在如下代码的写法中:
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("Report", "report", "~/Content/page/report.aspx")
routes.MapRoute( name: "BaseManage", url: "Admin/BaseManage/{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
routes.MapRoute( name: "Order", url: "Admin/OrderManage/{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
routes.MapRoute( name: "Admin", url: "Admin/{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
}
}
因为系统需要,方法RegisterRoutes中同时应用了MapPageRoute和MapRoute方法,这样写的后果就是所有的页面加载时都跳转到了WEBFORM页面中,导致系统报错。
报错原因是两种不同的方式之间有冲突,解决这个问题,需要给MapPageRoute加个约束,这时就用到了IRouteConstraint接口,看如下代码:
public class MyCustomConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { return routeDirection == RouteDirection.IncomingRequest; } }
然后按如下修改语句即可
//routes.MapPageRoute("Report", "report", "~/Content/page/report.aspx"); routes.MapPageRoute("Report", "report", "~/Content/page/report.aspx", true, null, new RouteValueDictionary { { "outgoing", new MyCustomConstraint() } });