您的位置:首页 > 编程语言 > ASP

分享 [ASP.NET]几个常用的MVC设置

2016-11-17 17:55 453 查看
一.默认路由(MVC自带)

复制代码

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 } // 参数默认值 (UrlParameter.Optional-可选的意思)

    ); 

  } 

复制代码

二.不带参数的路由

 routes.MapRoute

(

"NoParameter", 

"{controller}/{action}/{id}"

);

三.带命名空间的路由

 routes.MapRoute(

              "AdminControllers", // 路由名称

              "{controller}/{id}-{action}", // 带有参数的 URL

              new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // 参数默认值

              new string[] { "Admin.Controllers" }//命名空间

          );

四.带约束的路由规则(约束的意思就是用正则这类约束必须符合条件才可以)

routes.MapRoute(

                "RuleControllers",

                "{controller}/{action}-{Year}-{Month}-{Day}}",

                new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },

                new { Year = @"^\d{4}", Month = @"\d{2}" } //4位数 2位数

            );

五.带名称空间,带约束,带默认值的路由规则

复制代码

 routes.MapRoute(

                "Rule1",

                "Admin/{controller}/{action}-{Year}-{Month}-{Day}",

                new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },

                new { Year = @"^\d{4}", Month = @"\d{2}" },

                new string[] { "Admin.Controllers" }

            );

复制代码

六.捕获所有的路由

  routes.MapRoute(

                "All", // 路由名称

                "{*Vauler}", // 带有参数的 URL

                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值

            );
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息