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() } });
  • 相关阅读:
    oracle数据库导出与导入
    Mysql导入表信息[Err] 1067
    Golang--不定参数类型
    (转)Docker容器的重启策略及docker run的--restart选项详解
    (转)Golang--使用iota(常量计数器)
    Golang--匿名变量
    Golang--Hello World
    Ubuntu Server16.04 配置网卡
    U盘安装ubuntu 16.04 遇到 gfxboot.c32:not a COM32R image boot 的解决方法
    ipfs私链服务
  • 原文地址:https://www.cnblogs.com/yuanfg/p/9015762.html
Copyright © 2011-2022 走看看