码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET MVC中的路由IRouteConstraint方法应用实例

时间:2018-05-09 19:29:49      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:mapr   url   eva   semanage   direction   orm   request   apr   port   

在如下代码的写法中:

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() } });

 

ASP.NET MVC中的路由IRouteConstraint方法应用实例

标签:mapr   url   eva   semanage   direction   orm   request   apr   port   

原文地址:https://www.cnblogs.com/yuanfg/p/9015762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!