码迷,mamicode.com
首页 > 其他好文 > 详细

用路由设置,记录下

时间:2014-07-16 20:22:53      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   strong   io   

 routes.MapRoute(
                   "Default_a",
                   "huhangfei/{pageindex}-{state}-{size}.html",
                     new { controller = "Home", action = "Index", pageindex = 1, state = 0 ,size=0},
                    new { pageindex = @"\d+", state = @"\d+", size = @"\d+" }
                );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

大家在学习MVC的过程,老是用到设置路由,但有6个常用路由,是大家经常用到的。

一.默认路由(MVC自带)

bubuko.com,布布扣
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-可选的意思)
    ); 
  } 
bubuko.com,布布扣


二.不带参数的路由

 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位数
            );

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

bubuko.com,布布扣
 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" }
            );
bubuko.com,布布扣

六.捕获所有的路由

  routes.MapRoute(
                "All", // 路由名称
                "{*Vauler}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

用路由设置,记录下,布布扣,bubuko.com

用路由设置,记录下

标签:style   blog   http   color   strong   io   

原文地址:http://www.cnblogs.com/huhangfei/p/3836809.html

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