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

一.默认路由(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 } // 参数默认值
            );
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值