zoukankan      html  css  js  c++  java
  • ASP.NET MVC中的路由IRouteConstraint方法应用实例

    在如下代码的写法中:

    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() } });
  • 相关阅读:
    考试中一元三次方程的解法
    变限积分求导公式--加上自己理解
    柯西中值定理
    sec x的积分及注意事项
    线性代数
    IntelliJ IDEA无法新建类解决办法
    idea中Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually.
    Windows 64位下安装Redis 以及 可视化工具Redis Desktop Manager的安装和使用
    使用@Param注解
    关于在方法里面使用泛型public static <T> T
  • 原文地址:https://www.cnblogs.com/yuanfg/p/9015762.html
Copyright © 2011-2022 走看看