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组件的两个重要的作用!

路由规则实例解析

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

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

 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 } // 参数默认值
);
}

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规则,名称空间

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

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

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

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

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

 routes.MapRoute(
"Rule1",
"{controller}/{action}-{Year}-{Month}-{Day}}",
new { controller = "Home", action = "Index", Year = "", Month = "", Day = "" },
new { Year = @"^\d{4}", Month = @"\d{2}" }
);

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

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

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

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

实例六:捕获所有的路由

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

最新文章

  1. sql的那些事(一)
  2. PHP 的 __FILE__ 常量
  3. 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars
  4. CentOS 6.3下PostgreSQL 的安装与配置
  5. Java面试试题
  6. 给label text 上色 && 给textfiled placeholder 上色
  7. MySQL数据库设计复习笔记及项目实战
  8. codecomb 2091【路径数量】
  9. 使用OFFSET-FETCH进行数据过滤
  10. Java 枚举7常见种用法(转)
  11. BotVS数字货币现货交易类库
  12. 智能合约语言 Solidity 教程系列4 - 数据存储位置分析
  13. 用shell脚本创建sqlite表并添加sql语句--通用
  14. button的后台点击事件
  15. 【BZOJ4873】[六省联考2017]寿司餐厅(网络流)
  16. 防火墙iptables的简单使用
  17. EF 跨库查询
  18. Spring+hibernate+mysql事物不回滚的原因以及处理
  19. INDEX--创建索引和删除索引时的SCH_M锁
  20. [HTML] H5在webApp中的注意事项

热门文章

  1. CALayer的m34 - 三维透视效果
  2. swift学习笔记1——基础部分
  3. Java中堆内存和栈内存详解2
  4. ZooKeeper:第三方客户端 ZKClient
  5. Spring:Aop before after afterReturn afterThrowing around 的原理
  6. Redis在游戏服务器中的应用
  7. RSA算法原理
  8. AC日记——楼房 codevs 2995
  9. Winform进程、线程
  10. Doctype作用?严格模式与混杂模式如何区分?它们有何意义?