MVC2路由的调试

来源:互联网 发布:java发短信代码 编辑:程序博客网 时间:2024/05/04 15:06

1.取得RouteDebug.dll

2.项目中新建文件夹Library

3.项目中添加RouteDebug的引用

4.打开Global.asax

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();


            RegisterRoutes(RouteTable.Routes);
   


   //添加下面这行代码,即可开启路由匹配调试,不需要时,注释即可
            RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
        }



5.通过MapRoute的第四个参数限制路由的控制器的命名空间
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                // 参数默认值
                , null,
                new string[] { "Mvc调试.Controllers" }
                //限制该路由的命名空间为"Mvc调试.Controllers",防止Controler类重名
            );


        }


6.范围大的命名空间放在后面,复杂的放在前面,因为路由的匹配规则是匹配了第一个,后面就不再匹配了


7.匹配所有路由,用于显示主页
            routes.MapRoute(
                "MyRoute",
                "{*CatchAll}",  //匹配所有,让它跳到默认的主页上
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );


8.用正则表达式控制controller的名字


            //routes.MapRoute(
            //    "myRoute2", // 路由名称
            //    "{action}-{controller}/{id}", // 带有参数的 URL
            //    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // 参数默认值
            //    new { controller=@"^\d+$"},       //用正则表达式控制controller的名字只能为数字
            //    new string[] { "MVCShow.Controllers" }
            //);
0 0
原创粉丝点击