【基础】MVC路由规则

RouteData解析过程

在ASP.NET MVC中,服务器收到来自客户端的请求后,会经过一些列的处理拿到请求的数据,比如在Pipeline 管线事件中,通过订阅适当的事件,将HttpContext作为参数传入HttpContextWrapper进行封装,然后取得当前路由集合的数据RouteData进行解析,拿到具体的参数,包括请求路径、请求的参数、IRouteHandler等,通过IRouteHandlerGetHttpHandler返回一个IHttpHandler对象,通过该对象的ProcessRequest对请求进行处理,然后控制器工厂通过RouteData中匹配的Controller进行反射构造一个ControllerController调用IControllerExcute方法,同样是通过反射拿到当前请求的Action,最后执行Action,返回客户端数据,完成本次的请求。整体流程图如下所示:

 

在这个过程中,RouteData中的路由 起到了很大的作用。Routing的作用:首先通过HTTP请求,并解析Url请求中ControllerAction以及附加数据,其次将识别出来的数据传递给ControllerAction(Controller的方法)。这是Routing组件的两个重要的作用!

路由规则实例解析

实例一:系统默认提供的路由格式

下面是系统给的默认代码:

 1 public static void RegisterRoutes(RouteCollection routes)
 2 {
 3     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 4 
 5     routes.MapRoute(
 6         "Default", // 路由名称
 7         "{controller}/{action}/{id}", // 带有参数的 URL
 8         new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
 9     );
10 }

Url格式为:http://localhost:80/home/index  对应规则为:{controller}/{action}/{id}  黑体部分就是对应部分。这还是有默认值的情况。
详细匹配应该为:http://localhost:80/Custom/Detials/1   。去掉主机域名,剩下的对应就是匹配Controller和Action了。通过Routing组件解析这个Url,Controller是Custom,Action是Detials。传递过去的Id是1。这就是使用了MapRoute( string name, string url, object defaults);这个方法的重载。

实例二:不使用默认值的Url路由规则

函数签名:MapRoute( string name, string url);

routes.MapRoute("没有默认值路由规则", "{controller}/{id}-{action}");

适合的Url例子:http://localhost:80/Custom/1-Detials  

它将不匹配http://localhost:80/

实例三:带名称空间的Url路由规则

函数签名MapRoute( string name, string url, string[] namespaces);//路由名,Url规则,名称空间

1 routes.MapRoute(
2             "MyUrl", // 路由名称
3             "{controller}/{id}-{action}", // 带有参数的 URL
4             new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // 参数默认值
5             new string[] { "MvcDemo.Controllers" }//命名空间
6 );

Url:http://localhost:0000/Custom/1-Detials

这个例子是带命名空间的路由规则,这在Aeras使用时非常有用。

实例四:带约束的路由规则

函数签名:MapRoute( string name, string url, object defaults, object constraints);//路由名,Url规则,默认值,名称空间

1 routes.MapRoute(
2              "Rule1",
3              "{controller}/{action}-{Year}-{Month}-{Day}}",
4              new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },
5              new { Year = @"^\d{4}", Month = @"\d{2}" }
6 );
实例五:带名称空间,带约束,带默认值的路由规则

函数签名:MapRoute( string name, string url, object defaults, object constraints, string[] namespaces);

1 routes.MapRoute(
2                  "Rule1",
3                  "Admin/{controller}/{action}-{Year}-{Month}-{Day}",
4                  new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },
5                  new { Year = @"^\d{4}", Month = @"\d{2}" },
6                  new string[] { "MvcDemo.Controllers" }
7 );

Url:http://localhost:14039/Admin/home/index-2010-01-21

实例六:捕获所有的路由
1 routes.MapRoute(
2                  "All", // 路由名称
3                 "{*Vauler}", // 带有参数的 URL
4                  new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
5  );
1 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");//是忽略这个规则的Url
1 AreaRegistration.RegisterAllAreas();//注册所有的Areas
2 RegisterRoutes(RouteTable.Routes);//注册自定义规则
3 
4 ////调试用语句,需要下载RouteDebug.dll,并添加引用!加入这句话后就可以测试Url路由了。
5 //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);

 

转载于:https://www.cnblogs.com/xhb-bky-blog/p/6185206.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值