找回密码
 立即注册
首页 业界区 业界 写自己的ASP.NET MVC框架(上)

写自己的ASP.NET MVC框架(上)

南宫玉英 2025-5-29 16:03:37
写了几篇细说之后,今天打算换换口味,还是来写代码吧。所以,这次博客将以实际的代码来展示在ASP.NET平台上开发自己的框架,我希望也能让您发现这并不是件难事。
我在前面的博客【用Asp.net写自己的服务框架】中,发布了一个用ASP.NET写的服务框架,那个框架我目前仍在使用中。近来,由于时常也会有人问我一些关于ASP.NET MVC的话题,因此,就想再写个自己的MVC框架出来,一方面可以留给自己使用,另外也可以谈谈MVC,尤其可以展示一下在ASP.NET下写框架的乐趣。
我之所以将写框架看成是件有乐趣的事,是因为:在写框架的过程中会接触许多的技术细节。
比如:
1. 为了支持Session需要了解管线过程以及支持Session工作的原理。
2. 在HttpHandler的映射过程中,HttpHandlerFactory的使用会让设计很灵活。
3. 反射可以让我们轻松地根据一个URL去寻找匹配的Action以及为Action准备传入参数。
4. 为了便于测试Action,如何有效的封装框架的功能(这里有许多ASP.NET的技术细节)。
5. 如何设计让框架更灵活更强大。
在开始今天的博客之前,我想有必要说说我的框架的规模:
如果说ASP.NET WebForm是个复杂框架,ASP.NET MVC是个轻量级框架的话,那么,我的MVC框架将只是个微量级的框架。
但这个微量级的框架却可以提供许多实用的功能(因为我没有引入一些与MVC无关的东西),而且完全遵守MVC的思想而设计。
由于我的框架规模实在太小,因此,有些地方的确是不够完善,但我认为在大多数情况下是够用的。
ASP.NET程序的几种开发方式

时常见到有人在问:到底选择WebForm还是MVC ?
其实我认为最好是二个都选吧。然后,你去发现它们的优点与缺点,最后,当你觉得它们都不爽时,还是写自己的框架吧。
我来说说我这样选择的理由:任何东西都有它们的优点,这很正常,所以二个都选就能发现更多的优点,发现优点的过程其实也是自己进步的过程。当然它们也有缺点,发现那些缺点,你自然会想办法去避开,其实这也是个进步的过程。因此,在你吸收优点以及避开缺点的过程中,会感觉它们不再完美(因为自己在进步),再到后来,你会怎么选择,我就不知道了,那就是你自己的事了。而我选择了另一条路:写自己的ASP.NET MVC框架。
在比较各类框架之前,我想有必要先来总结一下:现在能用ASP.NET开发哪些类型的网站?由于ASP.NET与WCF这类纯服务性质的框架不同,我们主要还是用它来开发一些可与用户交互的界面程序。因此,今天的分类将重要讨论这些界面(UI)的开发方式。
我认为目前的ASP.NET能支持开发三种类型的网站:
1. 以服务端为中心的网站:所有页面的生成以及交互的逻辑全部服务端来完成,服务端甚至能生成必要的JS代码。
2. 门户类网站:服务端只负责页面的第一次呈现,用户的交互以及表单的提交全部采用AJAX的方式完成。
3. 纯AJAX网站:服务端基本上不参与UI的处理,只负责处理数据,UI在客户端由JavaScript来生成并控制提交。
【以服务端为中心的网站】,这类网站有个非常明显的特点,至少在开发上表现地非常明显:服务端要做的事情很多,HTML的生成, 简单的JS交互,客户端验证,等等,全由服务端来处理。在开发这类网站时,由于工作全在服务端,因此如果我们使用ASP.NET开发,自然地,所有的任务都将由aspx, C#这类技术来实现,采用这种方式开发出来的网站,页面与用户的交互性通常不会很友好,比如:提交数据时经常需要整页刷新。
【门户类网站】,这类网站与之前的【以服务端为中心的网站】有个重要的差别:页面只是呈现数据,表单的提交全采用AJAX方式了。这样做的好处能将显示逻辑与数据更新逻辑有效的分离,不必纠缠在一起(可认为是二个通道),在这种开发模式下,由于页面只负责数据的显示,因此,只要能将业务逻辑尽可能地与UI分离,项目在维护上会容易很多,采用这种方式开发的网站,页面与用户交互的友好性会好很多,而且也不会影响SEO,因此被较多的门户网站采用。
【纯AJAX网站】,在这类网站中,服务端由于不参与UI处理,网站可以只是些静态的HTML文件,而且在设计页面时,只要留下一些占位符就可以了,UI元素全部由JS来生成。这类网站的客户端通常会选择一个JS的UI框架来支持。这类界面相对于前二种来说,会更漂亮,用户的操作体验也会更友好。但由于页面主要由JS来生成,对SEO的支持较差,因此,特别适合一些后台类的网站项目。
在前面所列出的三种开发方式中,前二种由于界面部分由服务端来实现,因此选择一个合适的框架,会对开发工作有着非常重要的影响(尤其是第一种)。但是,如果选择第三种方式,那么选择 WebForm 还是 MVC 真的都是浮云了,甚至还可以使用其它的服务框架来支持AJAX的调用。
喜欢微软的MVC框架的一些人,通常会列举一些WebForm中较为低级的缺点,从而把ASP.NET MVC框架说的很完美,而且是非它不可。这里,我不想谈论它们的优点与缺点,因为我前面已经说过了,在我看来,它们都有优点也同时有各自的缺点。今天,我只想先暂且忘记它们,来实现自己的框架。
开始吧,看看我的作品。
介绍我的MVC框架

我比较喜欢ASP.NET这个平台,因为它们扩展性实在太好了,在它上面,我可以容易地实现自己所需的东西,包括开发自己所需要的WEB框架。通过微软的ASP.NET MVC框架,也让我认识到MVC思想的优点,因此,我的WEB框架也将采用MVC思想来开发,因此,我把自己的这个框架称为【我的MVC框架:MyMVC】。今天的博客也将向您展示这个框架,同时我也会与大家一起分享我在开发框架过程中所使用到的一些技术(或者称为实现方式)。
为了让大家对我的MVC框架有个感性的认识,我准备了一个示例网站,网站提供二种完全不同的风格,分别采用【门户类网站】和【纯AJAX网站】的方式来开发。在示例网站的首页,程序会让您选择喜欢的界面风格来继续后面的操作,当然,您也可以随时在右上角切换风格。
1.png

我的MVC框架设计架构

在我的框架中,【页面请求】与【AJAX请求】是分开来实现的。
因为我前面以对开发方式做过分类,在开发【纯AJAX网站】时,那么就几乎没有页面请求了(或许有,但可以忽略),此时在服务端全是AJAX服务(我喜欢将AJAX的服务端实现称为服务)。
我将AJAX请求分开来处理是因为:我做的网站中,AJAX的使用非常多,数量都会超过页面请求,而且有时甚至没有ASPX页面,全是AJAX调用,所以我更看重AJAX。
二种请求(我称为通道)大致是这样的处理过程:
2.png

说明:示意图中并没有直观地反映出【页面请求】与【AJAX请求】在处理过程中的差别,但这个差别是存在的,差别主要在于从URL到Action的映射过程,后面会有详细地介绍。
以下示意图表示了【我的MVC框架】在处理一个请求时的具体过程:
3.png


今天的博客内容将主要介绍这个框架如何实现AJAX请求处理,页面请求的实现过程将在后续的博客中介绍。

回忆以往AJAX的实现方式

我的MVC框架对AJAX的支持来源于我对代码不断重构的过程,为了更好地了解我的MVC框架,我认为有必要先来回忆一下以往是如何(在服务端)实现AJAX的。
在ASP.NET中有一种比较原始的实现Ajax的方式,那就是创建一个ashx,就像下面的代码:
  1. <%@ WebHandler Language="C#" Class="Handler1" %>
  2. using System;
  3. using System.Web;
  4. public class Handler1 : IHttpHandler {
  5. <httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>
  10. <httpHandlers>
  11. <httpHandlers>
  12.    
  13. </httpHandlers>
  14. </httpHandlers>public void ProcssRequest (HttpContext context) {
  15. <httpHandlers>
  16. <httpHandlers>
  17.    
  18. </httpHandlers>
  19. </httpHandlers><httpHandlers>
  20. <httpHandlers>
  21.    
  22. </httpHandlers>
  23. </httpHandlers>// 【1】. 从context.Request中读取输入参数
  24. <httpHandlers>
  25. <httpHandlers>
  26.    
  27. </httpHandlers>
  28. </httpHandlers><httpHandlers>
  29. <httpHandlers>
  30.    
  31. </httpHandlers>
  32. </httpHandlers>string param1 = context.Request.QueryString["param1"];
  33. <httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers><httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers>string param2 = context.Request.QueryString["param2"];
  42. <httpHandlers>
  43. <httpHandlers>
  44.    
  45. </httpHandlers>
  46. </httpHandlers><httpHandlers>
  47. <httpHandlers>
  48.    
  49. </httpHandlers>
  50. </httpHandlers>
  51. <httpHandlers>
  52. <httpHandlers>
  53.    
  54. </httpHandlers>
  55. </httpHandlers><httpHandlers>
  56. <httpHandlers>
  57.    
  58. </httpHandlers>
  59. </httpHandlers>// 【2】. 根据上面所获取的参数,调用服务层或者BLL层获取结果
  60. <httpHandlers>
  61. <httpHandlers>
  62.    
  63. </httpHandlers>
  64. </httpHandlers><httpHandlers>
  65. <httpHandlers>
  66.    
  67. </httpHandlers>
  68. </httpHandlers>// var result = CallXxxxMethod(param1, param2);
  69. <httpHandlers>
  70. <httpHandlers>
  71.    
  72. </httpHandlers>
  73. </httpHandlers><httpHandlers>
  74. <httpHandlers>
  75.    
  76. </httpHandlers>
  77. </httpHandlers>// 【3】. 将结果写入context.Response
  78. <httpHandlers>
  79. <httpHandlers>
  80.    
  81. </httpHandlers>
  82. </httpHandlers><httpHandlers>
  83. <httpHandlers>
  84.    
  85. </httpHandlers>
  86. </httpHandlers>context.Response.ContentType = "text/plain";
  87. <httpHandlers>
  88. <httpHandlers>
  89.    
  90. </httpHandlers>
  91. </httpHandlers><httpHandlers>
  92. <httpHandlers>
  93.    
  94. </httpHandlers>
  95. </httpHandlers>context.Response.Write(" result ...... ");
  96. <httpHandlers>
  97. <httpHandlers>
  98.    
  99. </httpHandlers>
  100. </httpHandlers>}
  101. <httpHandlers>
  102. <httpHandlers>
  103.    
  104. </httpHandlers>
  105. </httpHandlers>public bool IsReusable { get { return false; } }
  106. }
复制代码
当然了,也有人会选择创建一个空的aspx去代替ashx,而且使用aspx还可以只输出一个HTML片段。
在这种原始的方式下,整个处理过程可以大致分为注释中所标注的三个阶段。如果使用这种方式去做服务端的AJAX开发,当AJAX的数量到达一定规模后,可以发现:大量的代码是类似。我之所以称为【类似】,是因为它们却实有差别,差别在于:参数的名字不同,参数的类型不同,参数的个数不同,要调用的方法以及返回值不同。
说实话,这种机械代码我也写过。
不过,当我发现时这个现象时,我就开始想办法去解决这个问题,因为我非常不喜欢写这类重复性质的代码。
在重构过程中,也逐渐形成了我自己的AJAX服务端框架。
后来我把它写到我的第一篇博客中了:【晒晒我的Ajax服务端框架】
在AJAX的发展过程中,微软曾经推出过ASP.NET AJAX框架,它可以在服务端生成一些JS的代理类,让客户端的JS方便地调用服务端的方法。虽然那个框架设计地很巧妙,并且与WebForm配合地很完美,只可惜那个框架不够流行。后来的WCF通过一些配置也可以让JS去调用,不过,喜欢的人也不多,可能还是因为配置麻烦的缘故吧。当后来微软推出了ASP.NET MVC框架时,一些人开始惊呼:AJAX非ASP.NET MVC框架不可。因为ASP.NET MVC框架可以很容易让JS去调用一个C#方法,从此以后,再也不用去【读参数,调用方法,写输出】这些繁琐的事情了,而且没有WCF那么复杂的配置。的确,他们没有解决的问题,ASP.NET MVC框架很好地解决了。
今天的博客,我将向大家介绍我的AJAX解决方案,它同样可以很好的解决上面的那些繁琐的过程。
MyMVC中实现AJAX的方式

在我的框架中,服务端可以很容易地将一个C#方法公开给客户端的JavaScript来访问,比如下面这个C#方法:
  1. public class AjaxOrder
  2. {
  3. <httpHandlers>
  4. <httpHandlers>
  5.    
  6. </httpHandlers>
  7. </httpHandlers>[Action]
  8. <httpHandlers>
  9. <httpHandlers>
  10.    
  11. </httpHandlers>
  12. </httpHandlers>public void AddOrder(OrderSubmitForm form)
  13. <httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>{
  18. <httpHandlers>
  19. <httpHandlers>
  20.    
  21. </httpHandlers>
  22. </httpHandlers><httpHandlers>
  23. <httpHandlers>
  24.    
  25. </httpHandlers>
  26. </httpHandlers>Order order = form.ConvertToOrderItem();
  27. <httpHandlers>
  28. <httpHandlers>
  29.    
  30. </httpHandlers>
  31. </httpHandlers><httpHandlers>
  32. <httpHandlers>
  33.    
  34. </httpHandlers>
  35. </httpHandlers>BllFactory.GetOrderBLL().AddOrder(order);
  36. <httpHandlers>
  37. <httpHandlers>
  38.    
  39. </httpHandlers>
  40. </httpHandlers>}
复制代码
那么客户端就可以通过这个URL地址来调用那个方法:"/AjaxOrder/AddOrder.cspx" ,
URL中的一些名称与C#类名以及方法的名称的对应关系,请参考下图。
至于C#方法所需的参数,你就不用担心了,框架会替您准备好,你只要访问就可以了。
说明:这个Action太简单了,连返回值也没有。后面会有返回值的示例代码,请继续阅读。
前面的示例可以用下面的图形来表示C#代码与URL的映射关系:
4.png

补充说明一下:按照MVC的标准术语,下文将这类用于处理请求的方法将称为【Action】,Action所在的类型称为Controller。不过,在我的MVC框架中,Action又分【PageAction】和【AjaxAction】。而且,在我的MVC框架中,对Controller限制极少,不会要求您继承什么类型或者实现什么接口,Controller甚至可以是个静态类。
唯独只要求:1. 包含AjaxAction的Controller必须以Ajax开头包含PageAction的Controller必须以Controller结尾(照顾喜欢微软MVC框架的用户)。加这个限制仅仅是为了快速定位Action,并没有其它原因。2. 类型与方法的可见性为 public (同样仅仅只是为了快速定位) 。
所以,在我的框架中,Controller的意义将只是一个Action的容器。
如何使用MyMVC框架中的AJAX功能

在我的MVC框架中,JS几乎可以透明地直接调用C#方法。比如我有这样一个C#方法:
  1. public class AjaxDemo
  2. {
  3. <httpHandlers>
  4. <httpHandlers>
  5.    
  6. </httpHandlers>
  7. </httpHandlers>[Action]
  8. <httpHandlers>
  9. <httpHandlers>
  10.    
  11. </httpHandlers>
  12. </httpHandlers>public string GetMd5(string input)
  13. <httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>{
  18. <httpHandlers>
  19. <httpHandlers>
  20.    
  21. </httpHandlers>
  22. </httpHandlers><httpHandlers>
  23. <httpHandlers>
  24.    
  25. </httpHandlers>
  26. </httpHandlers>if( input == null )
  27. <httpHandlers>
  28. <httpHandlers>
  29.    
  30. </httpHandlers>
  31. </httpHandlers><httpHandlers>
  32. <httpHandlers>
  33.    
  34. </httpHandlers>
  35. </httpHandlers><httpHandlers>
  36. <httpHandlers>
  37.    
  38. </httpHandlers>
  39. </httpHandlers>input = string.Empty;
  40. <httpHandlers>
  41. <httpHandlers>
  42.    
  43. </httpHandlers>
  44. </httpHandlers><httpHandlers>
  45. <httpHandlers>
  46.    
  47. </httpHandlers>
  48. </httpHandlers>byte[] bb = (new MD5CryptoServiceProvider()).ComputeHash(Encoding.Default.GetBytes(input));
  49. <httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers><httpHandlers>
  54. <httpHandlers>
  55.    
  56. </httpHandlers>
  57. </httpHandlers>return BitConverter.ToString(bb).Replace("-", "").ToLower();
  58. <httpHandlers>
  59. <httpHandlers>
  60.    
  61. </httpHandlers>
  62. </httpHandlers>}
  63. }
复制代码
方法很简单,可以计算一个字符串的MD5值。下面再来看一下如何在JS中调用:
  1. $("#btnGetMd5").click(function(){
  2. <httpHandlers>
  3. <httpHandlers>
  4.    
  5. </httpHandlers>
  6. </httpHandlers>$.ajax({
  7. <httpHandlers>
  8. <httpHandlers>
  9.    
  10. </httpHandlers>
  11. </httpHandlers><httpHandlers>
  12. <httpHandlers>
  13.    
  14. </httpHandlers>
  15. </httpHandlers>// 以下二个URL地址都是有效的。
  16. <httpHandlers>
  17. <httpHandlers>
  18.    
  19. </httpHandlers>
  20. </httpHandlers><httpHandlers>
  21. <httpHandlers>
  22.    
  23. </httpHandlers>
  24. </httpHandlers>//url: "/AjaxDemo/GetMd5.cspx",
  25. <httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers><httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers>url: "/AjaxDemo.GetMd5.cspx",
  34. <httpHandlers>
  35. <httpHandlers>
  36.    
  37. </httpHandlers>
  38. </httpHandlers><httpHandlers>
  39. <httpHandlers>
  40.    
  41. </httpHandlers>
  42. </httpHandlers>data: {input: $("#txtInput").val()},
  43. <httpHandlers>
  44. <httpHandlers>
  45.    
  46. </httpHandlers>
  47. </httpHandlers><httpHandlers>
  48. <httpHandlers>
  49.    
  50. </httpHandlers>
  51. </httpHandlers>success: function(responseText){
  52. <httpHandlers>
  53. <httpHandlers>
  54.    
  55. </httpHandlers>
  56. </httpHandlers><httpHandlers>
  57. <httpHandlers>
  58.    
  59. </httpHandlers>
  60. </httpHandlers><httpHandlers>
  61. <httpHandlers>
  62.    
  63. </httpHandlers>
  64. </httpHandlers>$("#spanReslt").text(responseText);
  65. <httpHandlers>
  66. <httpHandlers>
  67.    
  68. </httpHandlers>
  69. </httpHandlers><httpHandlers>
  70. <httpHandlers>
  71.    
  72. </httpHandlers>
  73. </httpHandlers>}
  74. <httpHandlers>
  75. <httpHandlers>
  76.    
  77. </httpHandlers>
  78. </httpHandlers>});
  79. });
复制代码
说明一下:这里我使用JQuery这个JavaScript类库来完成客户端的部分。
在JS代码中,我通过一个URL地址就可以直接访问到前面所定义的C#方法,C#方法所面的参数由$.ajax()的data参数指定。由于实在过于简单,我感觉不需要再对这个示例做更多的解释。
唯独我要提醒的是:为了安全,JS并不能调用任何一个C#方法(虽然在技术上没有任何难度)。所以,如果您允许一个C#方法公开给JS调用,那么方法必须加[Action]这个Attribute 。
在前面的示例中,方法的传入参数以及返回值的类型都比较简单,事实上,MyMVC也可以支持复杂的数据类型。例如,以下方法的签名都是有效的:
  1. [Action]
  2. public void Insert(Customer customer)
  3. [Action]
  4. public object Delete(int id, string returnUrl)
  5. [Action]
  6. public object ShowCustomerPicker(string searchWord, int? page)
  7. [Action]
  8. public object Search(OrderSearchInfo info, int? page)
  9. [Action]
  10. public object ShowProductPicker(int? categoryId, string searchWord, int? page)
  11. [Action]
  12. public object LoadModel()
复制代码
有了MyMVC,就几乎上不需要再去访问QueryString,Form这些对象了。你需要什么参数,只要写在方法的签名中就可以了。参数可以是简单的数据类型,也可以是自定义的数据类型,参数的个数也没有限制。
不过,有一点我要提醒您:所有的数据来源只有二个地方:QueryString和Form,框架只读取这二个地方,而且直接访问它们的索引器。由于QueryString,Form这二个类型都是NameValueCollection,而NameValueCollection的索引器在实现上有点独特,因此请大家注意它们的返回值。关于NameValueCollection的细节描述,可以参考我的博客【细说 Request[]与Request.Params[]】,今天我就不再重谈这个细节话题了。
在读取参数时,万一出现key重复了怎么办?
框架还提供另一种解决方案,那就是您可以在C#的方法的签名中,声明NameValueCollection类型的变量,变量名可以从【Form,QueryString,Headers,ServerVariables】中选择。注意:对于后二者,框架本身也是不读取的,如果需要读取,只能使用这个方法来获取。示例代码如下:
  1. [Action]
  2. public string TestNameValueCollection(NameValueCollection queryString, NameValueCollection form,
  3. <httpHandlers>
  4. <httpHandlers>
  5.    
  6. </httpHandlers>
  7. </httpHandlers>NameValueCollection headers, NameValueCollection serverVariables)
  8. {
  9. <httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>StringBuilder sb = new StringBuilder();
  14. <httpHandlers>
  15. <httpHandlers>
  16.    
  17. </httpHandlers>
  18. </httpHandlers>foreach( string key in queryString.AllKeys )
  19. <httpHandlers>
  20. <httpHandlers>
  21.    
  22. </httpHandlers>
  23. </httpHandlers><httpHandlers>
  24. <httpHandlers>
  25.    
  26. </httpHandlers>
  27. </httpHandlers>sb.AppendFormat("queryString, {0} = {1}\r\n", key, queryString[key]);
  28. <httpHandlers>
  29. <httpHandlers>
  30.    
  31. </httpHandlers>
  32. </httpHandlers>foreach( string key in form.AllKeys )
  33. <httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers><httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers>sb.AppendFormat("form, {0} = {1}\r\n", key, form[key]);
  42. <httpHandlers>
  43. <httpHandlers>
  44.    
  45. </httpHandlers>
  46. </httpHandlers>foreach( string key in headers.AllKeys )
  47. <httpHandlers>
  48. <httpHandlers>
  49.    
  50. </httpHandlers>
  51. </httpHandlers><httpHandlers>
  52. <httpHandlers>
  53.    
  54. </httpHandlers>
  55. </httpHandlers>sb.AppendFormat("headers, {0} = {1}\r\n", key, headers[key]);
  56. <httpHandlers>
  57. <httpHandlers>
  58.    
  59. </httpHandlers>
  60. </httpHandlers>foreach( string key in serverVariables.AllKeys )
  61. <httpHandlers>
  62. <httpHandlers>
  63.    
  64. </httpHandlers>
  65. </httpHandlers><httpHandlers>
  66. <httpHandlers>
  67.    
  68. </httpHandlers>
  69. </httpHandlers>sb.AppendFormat("serverVariables, {0} = {1}\r\n", key, serverVariables[key]);
  70. <httpHandlers>
  71. <httpHandlers>
  72.    
  73. </httpHandlers>
  74. </httpHandlers>return sb.ToString();
  75. }
复制代码
代码中,我同时要求框架给出这四个集合,事实上,您可以根据实际情况来决定需要多少个参数。
注意:
1. 参数名称是大小写【不敏感】的。
2. 类型一定要求是NameValueCollection 。
3. 框架会优先读取QueryString,如果没有则会查看Form
4. 千万不要在Action中使用HttpContext.Current.Request.QueryString[]的方式读取来自客户端的参数。
关于参数,还有一种特殊的情况:我在博客【细说 Form (表单)】中曾提到过,例如,我有这样二个类型,它们的结构一样:
  1. public class Customer
  2. {
  3. <httpHandlers>
  4. <httpHandlers>
  5.    
  6. </httpHandlers>
  7. </httpHandlers>public string Name;
  8. <httpHandlers>
  9. <httpHandlers>
  10.    
  11. </httpHandlers>
  12. </httpHandlers>public string Tel;
  13. }
  14. public class Salesman
  15. {
  16. <httpHandlers>
  17. <httpHandlers>
  18.    
  19. </httpHandlers>
  20. </httpHandlers>public string Name { get; set; }
  21. <httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers>public string Tel { get; set; }
  26. }
复制代码
如果此时我有这样一个C#方法,又该如何处理呢?
  1. [Action]
  2. public string TestCustomerType(Customer customer, Salesman salesman)
  3. {
  4. <httpHandlers>
  5. <httpHandlers>
  6.    
  7. </httpHandlers>
  8. </httpHandlers>return "customer.Name = " + customer.Name + "\r\n" +
  9. <httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers><httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>"customer.Tel = " + customer.Tel + "\r\n" +
  18. <httpHandlers>
  19. <httpHandlers>
  20.    
  21. </httpHandlers>
  22. </httpHandlers><httpHandlers>
  23. <httpHandlers>
  24.    
  25. </httpHandlers>
  26. </httpHandlers>"salesman.Name = " + salesman.Name + "\r\n" +
  27. <httpHandlers>
  28. <httpHandlers>
  29.    
  30. </httpHandlers>
  31. </httpHandlers><httpHandlers>
  32. <httpHandlers>
  33.    
  34. </httpHandlers>
  35. </httpHandlers>"salesman.Name = " + salesman.Tel;
  36. }
复制代码
上面的示例也可以理解成:一模一样的参数类型,就是要出现多次,再或者,多个不同的自定义类型中,有些成员的名称是相同的。
此时我的框架在设计时与微软的MVC框架一样,要求在HTML中对name做特殊的设置,示例代码如下:
  1. <form action="/AjaxDemo2/TestCustomerType.cspx" method="post">
  2. <p>客户名称: <input type="text" name="customer.Name" style="width: 300px" /></p>
  3. <p>客户电话: <input type="text" name="customer.Tel" style="width: 300px" /></p>
  4. <p>销售员名称: <input type="text" name="salesman.Name" style="width: 300px" /></p>
  5. <p>销售员电话: <input type="text" name="salesman.Tel" style="width: 300px" /></p>
  6. <p><input type="submit" value="提交" /></p>
  7. </form>
复制代码
此时要求:input标签中的name必须能够反映C#方法的参数名以及类型中所包含的数据成员名称。
注意:在MyMVC框架中,自定义的数据类型所包含的数据成员不要求是属性,字段(Field)也是完全受支持的。
配置MyMVC框架

MyMVC框架在使用前,必须配置。
在前面的示例中,"/AjaxDemo2/TestCustomerType.cspx" 这样的URL地址,按照ASP.NET的默认设置,它是不能被映射到一个有效的处理器的,那时,将出现一个404异常。因此,为了使用MyMVC中对AJAX的支持,必须做以下配置:
  1. <httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>
复制代码
如果在IIS7的环境中运行,还需要以下配置:
  1. <httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers><httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers><httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers><httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers><httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers><httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers><httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers><httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers><httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers><httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers><httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers><httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers><httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers><httpHandlers>
  54. <httpHandlers>
  55.    
  56. </httpHandlers>
  57. </httpHandlers><httpHandlers>
  58. <httpHandlers>
  59.    
  60. </httpHandlers>
  61. </httpHandlers><httpHandlers>
  62. <httpHandlers>
  63.    
  64. </httpHandlers>
  65. </httpHandlers><httpHandlers>
  66. <httpHandlers>
  67.    
  68. </httpHandlers>
  69. </httpHandlers><httpHandlers>
  70. <httpHandlers>
  71.    
  72. </httpHandlers>
  73. </httpHandlers><httpHandlers>
  74. <httpHandlers>
  75.    
  76. </httpHandlers>
  77. </httpHandlers><httpHandlers>
  78. <httpHandlers>
  79.    
  80. </httpHandlers>
  81. </httpHandlers><httpHandlers>
  82. <httpHandlers>
  83.    
  84. </httpHandlers>
  85. </httpHandlers><httpHandlers>
  86. <httpHandlers>
  87.    
  88. </httpHandlers>
  89. </httpHandlers><httpHandlers>
  90. <httpHandlers>
  91.    
  92. </httpHandlers>
  93. </httpHandlers><httpHandlers>
  94. <httpHandlers>
  95.    
  96. </httpHandlers>
  97. </httpHandlers><httpHandlers>
  98. <httpHandlers>
  99.    
  100. </httpHandlers>
  101. </httpHandlers>
复制代码
在示例代码中,我使用了【cspx】这个扩展名,如果您不喜欢,也可以选择您所喜欢的扩展名,这个不是问题。
关于配置参数中的【path】属性,请参考我的上篇博客【细说 HttpHandler 的映射过程】,这里也不再重新解释。如果没有看过的,建议还是去看一下,下面将会用到那些知识,因为它非常重要。
MyMVC框架的实现原理 - 映射处理器(入口)

前面谈到了MyMVC框架的配置,通过那个配置,相当于在ASP.NET中为MyMVC注册了一个入口点。
根据上面的配置,符合条件的请求将会被映射给AjaxHandlerFactory。既然是这样,我们来看一下这个入口点的实现代码:
  1. internal sealed class AjaxHandlerFactory : IHttpHandlerFactory{<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>public IHttpHandler GetHandler(HttpContext context,<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers><httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers><httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers><httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers><httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers><httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers>string requestType, string virtualPath, string physicalPath)<httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers>{<httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers><httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers>// 根据请求路径,定位到要执行的Action<httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers><httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers>ControllerActionPair pair = UrlParser.ParseAjaxUrl(virtualPath);<httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers><httpHandlers>
  54. <httpHandlers>
  55.    
  56. </httpHandlers>
  57. </httpHandlers>if( pair == null )<httpHandlers>
  58. <httpHandlers>
  59.    
  60. </httpHandlers>
  61. </httpHandlers><httpHandlers>
  62. <httpHandlers>
  63.    
  64. </httpHandlers>
  65. </httpHandlers><httpHandlers>
  66. <httpHandlers>
  67.    
  68. </httpHandlers>
  69. </httpHandlers>ExceptionHelper.Throw404Exception(context);<httpHandlers>
  70. <httpHandlers>
  71.    
  72. </httpHandlers>
  73. </httpHandlers><httpHandlers>
  74. <httpHandlers>
  75.    
  76. </httpHandlers>
  77. </httpHandlers>// 获取内部表示的调用信息<httpHandlers>
  78. <httpHandlers>
  79.    
  80. </httpHandlers>
  81. </httpHandlers><httpHandlers>
  82. <httpHandlers>
  83.    
  84. </httpHandlers>
  85. </httpHandlers>InvokeInfo vkInfo = ReflectionHelper.GetAjaxInvokeInfo(pair);<httpHandlers>
  86. <httpHandlers>
  87.    
  88. </httpHandlers>
  89. </httpHandlers><httpHandlers>
  90. <httpHandlers>
  91.    
  92. </httpHandlers>
  93. </httpHandlers>if( vkInfo == null )<httpHandlers>
  94. <httpHandlers>
  95.    
  96. </httpHandlers>
  97. </httpHandlers><httpHandlers>
  98. <httpHandlers>
  99.    
  100. </httpHandlers>
  101. </httpHandlers><httpHandlers>
  102. <httpHandlers>
  103.    
  104. </httpHandlers>
  105. </httpHandlers>ExceptionHelper.Throw404Exception(context);<httpHandlers>
  106. <httpHandlers>
  107.    
  108. </httpHandlers>
  109. </httpHandlers><httpHandlers>
  110. <httpHandlers>
  111.    
  112. </httpHandlers>
  113. </httpHandlers>// 创建能够调用Action的HttpHandler<httpHandlers>
  114. <httpHandlers>
  115.    
  116. </httpHandlers>
  117. </httpHandlers><httpHandlers>
  118. <httpHandlers>
  119.    
  120. </httpHandlers>
  121. </httpHandlers>return ActionHandler.CreateHandler(vkInfo);<httpHandlers>
  122. <httpHandlers>
  123.    
  124. </httpHandlers>
  125. </httpHandlers>}<httpHandlers>
  126. <httpHandlers>
  127.    
  128. </httpHandlers>
  129. </httpHandlers><httpHandlers>
  130. <httpHandlers>
  131.    
  132. </httpHandlers>
  133. </httpHandlers>public void ReleaseHandler(IHttpHandler handler)<httpHandlers>
  134. <httpHandlers>
  135.    
  136. </httpHandlers>
  137. </httpHandlers>{<httpHandlers>
  138. <httpHandlers>
  139.    
  140. </httpHandlers>
  141. </httpHandlers>}}
复制代码
代码中,每个步骤做了什么事情,注释中有说明,不需要再重复说明。最后创建的ActionHandler的实现代码如下:
  1. internal class ActionHandler : IHttpHandler{<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>internal InvokeInfo InvokeInfo;<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>public void ProcessRequest(HttpContext context)<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>{<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers><httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers>// 调用核心的工具类,执行Action<httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers><httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers>ActionExecutor.ExecuteAction(context, this.InvokeInfo);<httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers>}<httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers>public bool IsReusable<httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers>{<httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers><httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers>get { return false; }<httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers>}
复制代码
整个入口点就是这样的。
有没人想过:为什么不直接在web.config中映射到这个ActionHandler呢?
答案在后面,请继续阅读。
MyMVC框架的实现原理 - 对Session的支持

前面有一个方法的实现我故意没有贴出,那么是ActionHandler.CreateHandler()这个静态方法。现在是时候来贴它了:
  1. public static ActionHandler CreateHandler(InvokeInfo vkInfo){<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>SessionMode mode = vkInfo.GetSessionMode();<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>if( mode == SessionMode.NotSupport )<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers><httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>return new ActionHandler { InvokeInfo = vkInfo };<httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers>else if( mode == SessionMode.ReadOnly )<httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers><httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers>return new ReadOnlySessionActionHandler { InvokeInfo = vkInfo };<httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers>else<httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers><httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers>return new RequiresSessionActionHandler { InvokeInfo = vkInfo };}
复制代码
这段代码又涉及另外二个类型,它们的实现代码如下:
  1. internal class RequiresSessionActionHandler : ActionHandler, IRequiresSessionState{}internal class ReadOnlySessionActionHandler :<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers><httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers><httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers><httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers><httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers><httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers> ActionHandler, IRequiresSessionState, IReadOnlySessionState{}
复制代码
不要感到奇怪,这二个类型的确没有任何代码。
它们除了从ActionHandler继承而来,还实现了另外二个接口,那二个接口我在博客【Session,有没有必要使用它?】中已有详细的解释,不明白的朋友,可以去阅读那篇博客。
再来回答前面那个问题:为什么不直接在web.config中映射到这个ActionHandler呢?
答:如果这样配置,那么对Session的支持将只有一种模式!
在这个框架中,我采用HttpHandlerFactory就可以轻松地实现对多种Session模式的支持。
说到这里,我真的感觉上篇博客【细说 HttpHandler 的映射过程】的研究成果太有意义了,是它给【MyMVC对Session完美的支持】提供了灵感。
老实说:我是不使用Session的。
但看到以前的博客中有些人还是坚持使用Session,所以就决定在MyMVC中支持这个功能,毕竟支持Session不是件难事。
下面再来说说如何支持Session 。
  1. [SessionMode(SessionMode.Support)][Action]public int TestSessionMode(int a){<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>// 一个累加的方法,检验是否可以访问Session<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>// 警告:示例代码的这样做法会影响Action的单元测试。<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>if( System.Web.HttpContext.Current.Session == null )<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers><httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers>throw new InvalidOperationException("Session没有开启。");<httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers>object obj = System.Web.HttpContext.Current.Session["counter"];<httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers>int counter = (obj == null ? 0 : (int)obj);<httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers>counter += a;<httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers>System.Web.HttpContext.Current.Session["counter"] = counter;<httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers>return counter;}
复制代码
在上面这段代码中,我加了一个[SessionMode]的Attribute,用它可以指定Action的Session支持模式,SessionMode是个枚举值,定义如下:
  1. /// /// Action所支持的Session模式/// public enum SessionMode{<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>///<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers> /// 不支持<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>///<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers> NotSupport,<httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers>///<httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers> /// 全支持<httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers>///<httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers> Support,<httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers>///<httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers> /// 仅支持读取<httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers>///<httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers> ReadOnly}
复制代码
MyMVC框架支持以上三种不同的Session模式,默认是关闭的,如果需要使用,请显式指定。[SessionMode]既可以用于Controller类型,也可以用于Action 。
注意:Session的使用将会给Action的单元测试带来麻烦。
MyMVC框架的实现原理 - 对OutputCache的支持

MyMVC框架对OutputCache也有着很好的支持。下面的代码演示了如何使用OutputCache:
  1. [OutputCache(Duration=10, VaryByParam="none")][Action]public string TestOutputCache(){<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>return DateTime.Now.ToString();}
复制代码
如果在浏览器中访问这个地址:http://localhost:34743/AjaxDemo/TestOutputCache.cspx
会发现结果在10秒钟内不会有改变(F5刷新),如果打开Fiddler2,会看到304的响应。
5.png

[OutputCache]所支持的属性较多,这里就不一一列出了,下面再来说说它的实现原理。
我在博客【细说 ASP.NET控制HTTP缓存】曾分析过ASP.NET Page的缓存页实现原理,其中有个小节【缓存页的服务端编程】专门分析了Page对OutputCache的实现过程,在MyMVC中,就是使用的这种方法,具体过程可以参考那篇博客。补充一句:微软的ASP.NET MVC也是这样做的,它也是借助了Page的强大功能。
MyMVC中的代码:
  1. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]public class OutputCacheAttribute : Attribute{<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>private OutputCacheParameters _cacheSettings = new OutputCacheParameters();<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>// 略过一些属性。<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>internal void SetResponseCache(HttpContext context)<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>{<httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers><httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers>if( context == null )<httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers><httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers><httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers>throw new ArgumentNullException("context");<httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers><httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers>OutputCachedPage page = new OutputCachedPage(_cacheSettings);<httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers><httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers>page.ProcessRequest(context);<httpHandlers>
  54. <httpHandlers>
  55.    
  56. </httpHandlers>
  57. </httpHandlers>}<httpHandlers>
  58. <httpHandlers>
  59.    
  60. </httpHandlers>
  61. </httpHandlers>private sealed class OutputCachedPage : Page<httpHandlers>
  62. <httpHandlers>
  63.    
  64. </httpHandlers>
  65. </httpHandlers>{<httpHandlers>
  66. <httpHandlers>
  67.    
  68. </httpHandlers>
  69. </httpHandlers><httpHandlers>
  70. <httpHandlers>
  71.    
  72. </httpHandlers>
  73. </httpHandlers>private OutputCacheParameters _cacheSettings;<httpHandlers>
  74. <httpHandlers>
  75.    
  76. </httpHandlers>
  77. </httpHandlers><httpHandlers>
  78. <httpHandlers>
  79.    
  80. </httpHandlers>
  81. </httpHandlers>public OutputCachedPage(OutputCacheParameters cacheSettings)<httpHandlers>
  82. <httpHandlers>
  83.    
  84. </httpHandlers>
  85. </httpHandlers><httpHandlers>
  86. <httpHandlers>
  87.    
  88. </httpHandlers>
  89. </httpHandlers>{<httpHandlers>
  90. <httpHandlers>
  91.    
  92. </httpHandlers>
  93. </httpHandlers><httpHandlers>
  94. <httpHandlers>
  95.    
  96. </httpHandlers>
  97. </httpHandlers><httpHandlers>
  98. <httpHandlers>
  99.    
  100. </httpHandlers>
  101. </httpHandlers>this.ID = Guid.NewGuid().ToString();<httpHandlers>
  102. <httpHandlers>
  103.    
  104. </httpHandlers>
  105. </httpHandlers><httpHandlers>
  106. <httpHandlers>
  107.    
  108. </httpHandlers>
  109. </httpHandlers><httpHandlers>
  110. <httpHandlers>
  111.    
  112. </httpHandlers>
  113. </httpHandlers>_cacheSettings = cacheSettings;<httpHandlers>
  114. <httpHandlers>
  115.    
  116. </httpHandlers>
  117. </httpHandlers><httpHandlers>
  118. <httpHandlers>
  119.    
  120. </httpHandlers>
  121. </httpHandlers>}<httpHandlers>
  122. <httpHandlers>
  123.    
  124. </httpHandlers>
  125. </httpHandlers><httpHandlers>
  126. <httpHandlers>
  127.    
  128. </httpHandlers>
  129. </httpHandlers>protected override void FrameworkInitialize()<httpHandlers>
  130. <httpHandlers>
  131.    
  132. </httpHandlers>
  133. </httpHandlers><httpHandlers>
  134. <httpHandlers>
  135.    
  136. </httpHandlers>
  137. </httpHandlers>{<httpHandlers>
  138. <httpHandlers>
  139.    
  140. </httpHandlers>
  141. </httpHandlers><httpHandlers>
  142. <httpHandlers>
  143.    
  144. </httpHandlers>
  145. </httpHandlers><httpHandlers>
  146. <httpHandlers>
  147.    
  148. </httpHandlers>
  149. </httpHandlers>base.FrameworkInitialize();<httpHandlers>
  150. <httpHandlers>
  151.    
  152. </httpHandlers>
  153. </httpHandlers><httpHandlers>
  154. <httpHandlers>
  155.    
  156. </httpHandlers>
  157. </httpHandlers><httpHandlers>
  158. <httpHandlers>
  159.    
  160. </httpHandlers>
  161. </httpHandlers>InitOutputCache(_cacheSettings);<httpHandlers>
  162. <httpHandlers>
  163.    
  164. </httpHandlers>
  165. </httpHandlers><httpHandlers>
  166. <httpHandlers>
  167.    
  168. </httpHandlers>
  169. </httpHandlers>}<httpHandlers>
  170. <httpHandlers>
  171.    
  172. </httpHandlers>
  173. </httpHandlers>}}
复制代码
在OutputCacheAttribute类的用法中,清楚地指出适用于类型与方法,因此,这个Attribute可以用于Controller和Action 。
说明:OutputCacheAttribute与SessionModeAttribute类似,都可以用于Controller和Action,同时使用时,Action优先匹配,代码如下:
  1. internal sealed class InvokeInfo{<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>public ControllerDescription Controller;<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>public ActionDescription Action;<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>public object Instance;<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>public OutputCacheAttribute GetOutputCacheSetting()<httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers>{<httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers><httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers>if( this.Action != null && this.Action.OutputCache != null )<httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers><httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers><httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers>return this.Action.OutputCache;<httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers><httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers>if( this.Controller != null && this.Controller.OutputCache != null )<httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers><httpHandlers>
  54. <httpHandlers>
  55.    
  56. </httpHandlers>
  57. </httpHandlers><httpHandlers>
  58. <httpHandlers>
  59.    
  60. </httpHandlers>
  61. </httpHandlers>return this.Controller.OutputCache;<httpHandlers>
  62. <httpHandlers>
  63.    
  64. </httpHandlers>
  65. </httpHandlers><httpHandlers>
  66. <httpHandlers>
  67.    
  68. </httpHandlers>
  69. </httpHandlers><httpHandlers>
  70. <httpHandlers>
  71.    
  72. </httpHandlers>
  73. </httpHandlers><httpHandlers>
  74. <httpHandlers>
  75.    
  76. </httpHandlers>
  77. </httpHandlers><httpHandlers>
  78. <httpHandlers>
  79.    
  80. </httpHandlers>
  81. </httpHandlers>return null;<httpHandlers>
  82. <httpHandlers>
  83.    
  84. </httpHandlers>
  85. </httpHandlers>}<httpHandlers>
  86. <httpHandlers>
  87.    
  88. </httpHandlers>
  89. </httpHandlers>public SessionMode GetSessionMode()<httpHandlers>
  90. <httpHandlers>
  91.    
  92. </httpHandlers>
  93. </httpHandlers>{<httpHandlers>
  94. <httpHandlers>
  95.    
  96. </httpHandlers>
  97. </httpHandlers><httpHandlers>
  98. <httpHandlers>
  99.    
  100. </httpHandlers>
  101. </httpHandlers>if( this.Action != null && this.Action.SessionMode != null )<httpHandlers>
  102. <httpHandlers>
  103.    
  104. </httpHandlers>
  105. </httpHandlers><httpHandlers>
  106. <httpHandlers>
  107.    
  108. </httpHandlers>
  109. </httpHandlers><httpHandlers>
  110. <httpHandlers>
  111.    
  112. </httpHandlers>
  113. </httpHandlers>return this.Action.SessionMode.SessionMode;<httpHandlers>
  114. <httpHandlers>
  115.    
  116. </httpHandlers>
  117. </httpHandlers><httpHandlers>
  118. <httpHandlers>
  119.    
  120. </httpHandlers>
  121. </httpHandlers>if( this.Controller != null && this.Controller.SessionMode != null )<httpHandlers>
  122. <httpHandlers>
  123.    
  124. </httpHandlers>
  125. </httpHandlers><httpHandlers>
  126. <httpHandlers>
  127.    
  128. </httpHandlers>
  129. </httpHandlers><httpHandlers>
  130. <httpHandlers>
  131.    
  132. </httpHandlers>
  133. </httpHandlers>return this.Controller.SessionMode.SessionMode;<httpHandlers>
  134. <httpHandlers>
  135.    
  136. </httpHandlers>
  137. </httpHandlers><httpHandlers>
  138. <httpHandlers>
  139.    
  140. </httpHandlers>
  141. </httpHandlers><httpHandlers>
  142. <httpHandlers>
  143.    
  144. </httpHandlers>
  145. </httpHandlers><httpHandlers>
  146. <httpHandlers>
  147.    
  148. </httpHandlers>
  149. </httpHandlers><httpHandlers>
  150. <httpHandlers>
  151.    
  152. </httpHandlers>
  153. </httpHandlers>return SessionMode.NotSupport;<httpHandlers>
  154. <httpHandlers>
  155.    
  156. </httpHandlers>
  157. </httpHandlers>}}
复制代码
因此,框架只要选择一个时机调用SetResponseCache()方法就可以了,至于这个调用时机出现在哪里,请继续阅读。
MyMVC框架的实现原理 - 查找Action的过程

前面有张图片反映了从URL地址到Action的映射过程:
6.png

下面再来谈谈这个过程的实现。
首先,我们要先在web.config中注册MyMVC的HttpHandlerFactory,它是整个框架的入口。
在ASP.NET的管线过程中,会调用GetHandler()方法,终于我的代码有机会运行了!
框架执行的第一行代码是:
  1. // 根据请求路径,定位到要执行的Action
  2. ControllerActionPair pair = UrlParser.ParseAjaxUrl(virtualPath);
复制代码
ControllerActionPair是我定义的一个表示Controller以及Action名字的值对类型:
  1. public sealed class ControllerActionPair{<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>public string Controller;<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>public string Action;}
复制代码
静态方法UrlParser.ParseAjaxUrl()就是专门用来解析URL并返回ControllerActionPair的:
  1. internal static class UrlParser{<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>// 用于匹配Ajax请求的正则表达式,<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>// 可以匹配的URL:/AjaxClass/method.cspx?id=2<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>// 注意:类名必须Ajax做为前缀<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>internal static readonly string AjaxUrlPattern<httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers><httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers>= @"/(?(\w[\./\w]*)?(?=Ajax)\w+)[/\.](?\w+)\.[a-zA-Z]+";<httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers>public static ControllerActionPair ParseAjaxUrl(string path)<httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers>{<httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers><httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers>if( string.IsNullOrEmpty(path) )<httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers><httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers><httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers>throw new ArgumentNullException("path");<httpHandlers>
  54. <httpHandlers>
  55.    
  56. </httpHandlers>
  57. </httpHandlers><httpHandlers>
  58. <httpHandlers>
  59.    
  60. </httpHandlers>
  61. </httpHandlers>Match match = Regex.Match(path, AjaxUrlPattern);<httpHandlers>
  62. <httpHandlers>
  63.    
  64. </httpHandlers>
  65. </httpHandlers><httpHandlers>
  66. <httpHandlers>
  67.    
  68. </httpHandlers>
  69. </httpHandlers>if( match.Success == false )<httpHandlers>
  70. <httpHandlers>
  71.    
  72. </httpHandlers>
  73. </httpHandlers><httpHandlers>
  74. <httpHandlers>
  75.    
  76. </httpHandlers>
  77. </httpHandlers><httpHandlers>
  78. <httpHandlers>
  79.    
  80. </httpHandlers>
  81. </httpHandlers>return null;<httpHandlers>
  82. <httpHandlers>
  83.    
  84. </httpHandlers>
  85. </httpHandlers><httpHandlers>
  86. <httpHandlers>
  87.    
  88. </httpHandlers>
  89. </httpHandlers>return new ControllerActionPair {<httpHandlers>
  90. <httpHandlers>
  91.    
  92. </httpHandlers>
  93. </httpHandlers><httpHandlers>
  94. <httpHandlers>
  95.    
  96. </httpHandlers>
  97. </httpHandlers><httpHandlers>
  98. <httpHandlers>
  99.    
  100. </httpHandlers>
  101. </httpHandlers>Controller = match.Groups["name"].Value.Replace("/", "."),<httpHandlers>
  102. <httpHandlers>
  103.    
  104. </httpHandlers>
  105. </httpHandlers><httpHandlers>
  106. <httpHandlers>
  107.    
  108. </httpHandlers>
  109. </httpHandlers><httpHandlers>
  110. <httpHandlers>
  111.    
  112. </httpHandlers>
  113. </httpHandlers>Action = match.Groups["method"].Value<httpHandlers>
  114. <httpHandlers>
  115.    
  116. </httpHandlers>
  117. </httpHandlers><httpHandlers>
  118. <httpHandlers>
  119.    
  120. </httpHandlers>
  121. </httpHandlers>};<httpHandlers>
  122. <httpHandlers>
  123.    
  124. </httpHandlers>
  125. </httpHandlers>}}
复制代码
代码很简单,核心其实就是那个正则表达式,从URL中提取Controller,Action的名字全靠它。
至于正则表达式的使用,我想这是个基本功,这里就略过了。
再来看AjaxHandlerFactory的第二个调用:
  1. // 获取内部表示的调用信息
  2. InvokeInfo vkInfo = ReflectionHelper.GetAjaxInvokeInfo(pair);
复制代码
ReflectionHelper类是一个内部使用的工具类,专门用于反射处理,AjaxAction查找过程的相关代码如下(注意代码中的注释):
  1. internal static class ReflectionHelper{<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>// 保存AjaxController的列表<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>private static List s_AjaxControllerList;<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>// 保存AjaxAction的字典<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>private static Hashtable s_AjaxActionTable = Hashtable.Synchronized(<httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers><httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers><httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers><httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers><httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers><httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers><httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers><httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers><httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers><httpHandlers>
  54. <httpHandlers>
  55.    
  56. </httpHandlers>
  57. </httpHandlers>new Hashtable(4096, StringComparer.OrdinalIgnoreCase));<httpHandlers>
  58. <httpHandlers>
  59.    
  60. </httpHandlers>
  61. </httpHandlers>// 用于从类型查找Action的反射标记<httpHandlers>
  62. <httpHandlers>
  63.    
  64. </httpHandlers>
  65. </httpHandlers>private static readonly BindingFlags ActionBindingFlags =<httpHandlers>
  66. <httpHandlers>
  67.    
  68. </httpHandlers>
  69. </httpHandlers><httpHandlers>
  70. <httpHandlers>
  71.    
  72. </httpHandlers>
  73. </httpHandlers>BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase;<httpHandlers>
  74. <httpHandlers>
  75.    
  76. </httpHandlers>
  77. </httpHandlers>static ReflectionHelper()<httpHandlers>
  78. <httpHandlers>
  79.    
  80. </httpHandlers>
  81. </httpHandlers>{<httpHandlers>
  82. <httpHandlers>
  83.    
  84. </httpHandlers>
  85. </httpHandlers><httpHandlers>
  86. <httpHandlers>
  87.    
  88. </httpHandlers>
  89. </httpHandlers>InitControllers();<httpHandlers>
  90. <httpHandlers>
  91.    
  92. </httpHandlers>
  93. </httpHandlers>}<httpHandlers>
  94. <httpHandlers>
  95.    
  96. </httpHandlers>
  97. </httpHandlers>///<httpHandlers>
  98. <httpHandlers>
  99.    
  100. </httpHandlers>
  101. </httpHandlers> /// 加载所有的Controller<httpHandlers>
  102. <httpHandlers>
  103.    
  104. </httpHandlers>
  105. </httpHandlers>///<httpHandlers>
  106. <httpHandlers>
  107.    
  108. </httpHandlers>
  109. </httpHandlers> private static void InitControllers()<httpHandlers>
  110. <httpHandlers>
  111.    
  112. </httpHandlers>
  113. </httpHandlers>{<httpHandlers>
  114. <httpHandlers>
  115.    
  116. </httpHandlers>
  117. </httpHandlers><httpHandlers>
  118. <httpHandlers>
  119.    
  120. </httpHandlers>
  121. </httpHandlers>s_AjaxControllerList = new List(1024);<httpHandlers>
  122. <httpHandlers>
  123.    
  124. </httpHandlers>
  125. </httpHandlers><httpHandlers>
  126. <httpHandlers>
  127.    
  128. </httpHandlers>
  129. </httpHandlers>ICollection assemblies = BuildManager.GetReferencedAssemblies();<httpHandlers>
  130. <httpHandlers>
  131.    
  132. </httpHandlers>
  133. </httpHandlers><httpHandlers>
  134. <httpHandlers>
  135.    
  136. </httpHandlers>
  137. </httpHandlers>foreach( Assembly assembly in assemblies ) {<httpHandlers>
  138. <httpHandlers>
  139.    
  140. </httpHandlers>
  141. </httpHandlers><httpHandlers>
  142. <httpHandlers>
  143.    
  144. </httpHandlers>
  145. </httpHandlers><httpHandlers>
  146. <httpHandlers>
  147.    
  148. </httpHandlers>
  149. </httpHandlers>// 过滤以【System.】开头的程序集,加快速度<httpHandlers>
  150. <httpHandlers>
  151.    
  152. </httpHandlers>
  153. </httpHandlers><httpHandlers>
  154. <httpHandlers>
  155.    
  156. </httpHandlers>
  157. </httpHandlers><httpHandlers>
  158. <httpHandlers>
  159.    
  160. </httpHandlers>
  161. </httpHandlers>if( assembly.FullName.StartsWith("System.", StringComparison.OrdinalIgnoreCase) )<httpHandlers>
  162. <httpHandlers>
  163.    
  164. </httpHandlers>
  165. </httpHandlers><httpHandlers>
  166. <httpHandlers>
  167.    
  168. </httpHandlers>
  169. </httpHandlers><httpHandlers>
  170. <httpHandlers>
  171.    
  172. </httpHandlers>
  173. </httpHandlers><httpHandlers>
  174. <httpHandlers>
  175.    
  176. </httpHandlers>
  177. </httpHandlers>continue;<httpHandlers>
  178. <httpHandlers>
  179.    
  180. </httpHandlers>
  181. </httpHandlers><httpHandlers>
  182. <httpHandlers>
  183.    
  184. </httpHandlers>
  185. </httpHandlers><httpHandlers>
  186. <httpHandlers>
  187.    
  188. </httpHandlers>
  189. </httpHandlers>try {<httpHandlers>
  190. <httpHandlers>
  191.    
  192. </httpHandlers>
  193. </httpHandlers><httpHandlers>
  194. <httpHandlers>
  195.    
  196. </httpHandlers>
  197. </httpHandlers><httpHandlers>
  198. <httpHandlers>
  199.    
  200. </httpHandlers>
  201. </httpHandlers><httpHandlers>
  202. <httpHandlers>
  203.    
  204. </httpHandlers>
  205. </httpHandlers>foreach( Type t in assembly.GetExportedTypes() ) {<httpHandlers>
  206. <httpHandlers>
  207.    
  208. </httpHandlers>
  209. </httpHandlers><httpHandlers>
  210. <httpHandlers>
  211.    
  212. </httpHandlers>
  213. </httpHandlers><httpHandlers>
  214. <httpHandlers>
  215.    
  216. </httpHandlers>
  217. </httpHandlers><httpHandlers>
  218. <httpHandlers>
  219.    
  220. </httpHandlers>
  221. </httpHandlers><httpHandlers>
  222. <httpHandlers>
  223.    
  224. </httpHandlers>
  225. </httpHandlers>if( t.Name.StartsWith("Ajax") )<httpHandlers>
  226. <httpHandlers>
  227.    
  228. </httpHandlers>
  229. </httpHandlers><httpHandlers>
  230. <httpHandlers>
  231.    
  232. </httpHandlers>
  233. </httpHandlers><httpHandlers>
  234. <httpHandlers>
  235.    
  236. </httpHandlers>
  237. </httpHandlers><httpHandlers>
  238. <httpHandlers>
  239.    
  240. </httpHandlers>
  241. </httpHandlers><httpHandlers>
  242. <httpHandlers>
  243.    
  244. </httpHandlers>
  245. </httpHandlers><httpHandlers>
  246. <httpHandlers>
  247.    
  248. </httpHandlers>
  249. </httpHandlers>s_AjaxControllerList.Add(new ControllerDescription(t));<httpHandlers>
  250. <httpHandlers>
  251.    
  252. </httpHandlers>
  253. </httpHandlers><httpHandlers>
  254. <httpHandlers>
  255.    
  256. </httpHandlers>
  257. </httpHandlers><httpHandlers>
  258. <httpHandlers>
  259.    
  260. </httpHandlers>
  261. </httpHandlers><httpHandlers>
  262. <httpHandlers>
  263.    
  264. </httpHandlers>
  265. </httpHandlers>}<httpHandlers>
  266. <httpHandlers>
  267.    
  268. </httpHandlers>
  269. </httpHandlers><httpHandlers>
  270. <httpHandlers>
  271.    
  272. </httpHandlers>
  273. </httpHandlers><httpHandlers>
  274. <httpHandlers>
  275.    
  276. </httpHandlers>
  277. </httpHandlers>}<httpHandlers>
  278. <httpHandlers>
  279.    
  280. </httpHandlers>
  281. </httpHandlers><httpHandlers>
  282. <httpHandlers>
  283.    
  284. </httpHandlers>
  285. </httpHandlers><httpHandlers>
  286. <httpHandlers>
  287.    
  288. </httpHandlers>
  289. </httpHandlers>catch { }<httpHandlers>
  290. <httpHandlers>
  291.    
  292. </httpHandlers>
  293. </httpHandlers><httpHandlers>
  294. <httpHandlers>
  295.    
  296. </httpHandlers>
  297. </httpHandlers>}<httpHandlers>
  298. <httpHandlers>
  299.    
  300. </httpHandlers>
  301. </httpHandlers><httpHandlers>
  302. <httpHandlers>
  303.    
  304. </httpHandlers>
  305. </httpHandlers>// 用于Ajax调用的Action信息则采用延迟加载的方式。<httpHandlers>
  306. <httpHandlers>
  307.    
  308. </httpHandlers>
  309. </httpHandlers>}<httpHandlers>
  310. <httpHandlers>
  311.    
  312. </httpHandlers>
  313. </httpHandlers><httpHandlers>
  314. <httpHandlers>
  315.    
  316. </httpHandlers>
  317. </httpHandlers>///<httpHandlers>
  318. <httpHandlers>
  319.    
  320. </httpHandlers>
  321. </httpHandlers> /// 根据要调用的controller名返回对应的Controller (适用于Ajax调用)<httpHandlers>
  322. <httpHandlers>
  323.    
  324. </httpHandlers>
  325. </httpHandlers>///<httpHandlers>
  326. <httpHandlers>
  327.    
  328. </httpHandlers>
  329. </httpHandlers> ///<httpHandlers>
  330. <httpHandlers>
  331.    
  332. </httpHandlers>
  333. </httpHandlers> ///<httpHandlers>
  334. <httpHandlers>
  335.    
  336. </httpHandlers>
  337. </httpHandlers> private static ControllerDescription GetAjaxController(string controller)<httpHandlers>
  338. <httpHandlers>
  339.    
  340. </httpHandlers>
  341. </httpHandlers>{<httpHandlers>
  342. <httpHandlers>
  343.    
  344. </httpHandlers>
  345. </httpHandlers><httpHandlers>
  346. <httpHandlers>
  347.    
  348. </httpHandlers>
  349. </httpHandlers>if( string.IsNullOrEmpty(controller) )<httpHandlers>
  350. <httpHandlers>
  351.    
  352. </httpHandlers>
  353. </httpHandlers><httpHandlers>
  354. <httpHandlers>
  355.    
  356. </httpHandlers>
  357. </httpHandlers><httpHandlers>
  358. <httpHandlers>
  359.    
  360. </httpHandlers>
  361. </httpHandlers>throw new ArgumentNullException("controller");<httpHandlers>
  362. <httpHandlers>
  363.    
  364. </httpHandlers>
  365. </httpHandlers><httpHandlers>
  366. <httpHandlers>
  367.    
  368. </httpHandlers>
  369. </httpHandlers>// 查找类型的方式:如果有点号,则按全名来查找(包含命名空间),否则只看名字。<httpHandlers>
  370. <httpHandlers>
  371.    
  372. </httpHandlers>
  373. </httpHandlers><httpHandlers>
  374. <httpHandlers>
  375.    
  376. </httpHandlers>
  377. </httpHandlers>// 本框架对于多个匹配条件的类型,将返回第一个匹配项。<httpHandlers>
  378. <httpHandlers>
  379.    
  380. </httpHandlers>
  381. </httpHandlers><httpHandlers>
  382. <httpHandlers>
  383.    
  384. </httpHandlers>
  385. </httpHandlers>if( controller.IndexOf('.') > 0 )<httpHandlers>
  386. <httpHandlers>
  387.    
  388. </httpHandlers>
  389. </httpHandlers><httpHandlers>
  390. <httpHandlers>
  391.    
  392. </httpHandlers>
  393. </httpHandlers><httpHandlers>
  394. <httpHandlers>
  395.    
  396. </httpHandlers>
  397. </httpHandlers>return s_AjaxControllerList.FirstOrDefault(<httpHandlers>
  398. <httpHandlers>
  399.    
  400. </httpHandlers>
  401. </httpHandlers><httpHandlers>
  402. <httpHandlers>
  403.    
  404. </httpHandlers>
  405. </httpHandlers><httpHandlers>
  406. <httpHandlers>
  407.    
  408. </httpHandlers>
  409. </httpHandlers><httpHandlers>
  410. <httpHandlers>
  411.    
  412. </httpHandlers>
  413. </httpHandlers>t => string.Compare(t.ControllerType.FullName, controller, true) == 0);<httpHandlers>
  414. <httpHandlers>
  415.    
  416. </httpHandlers>
  417. </httpHandlers><httpHandlers>
  418. <httpHandlers>
  419.    
  420. </httpHandlers>
  421. </httpHandlers>else<httpHandlers>
  422. <httpHandlers>
  423.    
  424. </httpHandlers>
  425. </httpHandlers><httpHandlers>
  426. <httpHandlers>
  427.    
  428. </httpHandlers>
  429. </httpHandlers><httpHandlers>
  430. <httpHandlers>
  431.    
  432. </httpHandlers>
  433. </httpHandlers>return s_AjaxControllerList.FirstOrDefault(<httpHandlers>
  434. <httpHandlers>
  435.    
  436. </httpHandlers>
  437. </httpHandlers><httpHandlers>
  438. <httpHandlers>
  439.    
  440. </httpHandlers>
  441. </httpHandlers><httpHandlers>
  442. <httpHandlers>
  443.    
  444. </httpHandlers>
  445. </httpHandlers><httpHandlers>
  446. <httpHandlers>
  447.    
  448. </httpHandlers>
  449. </httpHandlers>t => string.Compare(t.ControllerType.Name, controller, true) == 0);<httpHandlers>
  450. <httpHandlers>
  451.    
  452. </httpHandlers>
  453. </httpHandlers>}<httpHandlers>
  454. <httpHandlers>
  455.    
  456. </httpHandlers>
  457. </httpHandlers>///<httpHandlers>
  458. <httpHandlers>
  459.    
  460. </httpHandlers>
  461. </httpHandlers> /// 根据要调用的方法名返回对应的 Action (适用于Ajax调用)<httpHandlers>
  462. <httpHandlers>
  463.    
  464. </httpHandlers>
  465. </httpHandlers>///<httpHandlers>
  466. <httpHandlers>
  467.    
  468. </httpHandlers>
  469. </httpHandlers> ///<httpHandlers>
  470. <httpHandlers>
  471.    
  472. </httpHandlers>
  473. </httpHandlers> ///<httpHandlers>
  474. <httpHandlers>
  475.    
  476. </httpHandlers>
  477. </httpHandlers> ///<httpHandlers>
  478. <httpHandlers>
  479.    
  480. </httpHandlers>
  481. </httpHandlers> private static ActionDescription GetAjaxAction(Type controller, string action)<httpHandlers>
  482. <httpHandlers>
  483.    
  484. </httpHandlers>
  485. </httpHandlers>{<httpHandlers>
  486. <httpHandlers>
  487.    
  488. </httpHandlers>
  489. </httpHandlers><httpHandlers>
  490. <httpHandlers>
  491.    
  492. </httpHandlers>
  493. </httpHandlers>if( controller == null )<httpHandlers>
  494. <httpHandlers>
  495.    
  496. </httpHandlers>
  497. </httpHandlers><httpHandlers>
  498. <httpHandlers>
  499.    
  500. </httpHandlers>
  501. </httpHandlers><httpHandlers>
  502. <httpHandlers>
  503.    
  504. </httpHandlers>
  505. </httpHandlers>throw new ArgumentNullException("controller");<httpHandlers>
  506. <httpHandlers>
  507.    
  508. </httpHandlers>
  509. </httpHandlers><httpHandlers>
  510. <httpHandlers>
  511.    
  512. </httpHandlers>
  513. </httpHandlers>if( string.IsNullOrEmpty(action) )<httpHandlers>
  514. <httpHandlers>
  515.    
  516. </httpHandlers>
  517. </httpHandlers><httpHandlers>
  518. <httpHandlers>
  519.    
  520. </httpHandlers>
  521. </httpHandlers><httpHandlers>
  522. <httpHandlers>
  523.    
  524. </httpHandlers>
  525. </httpHandlers>throw new ArgumentNullException("action");<httpHandlers>
  526. <httpHandlers>
  527.    
  528. </httpHandlers>
  529. </httpHandlers><httpHandlers>
  530. <httpHandlers>
  531.    
  532. </httpHandlers>
  533. </httpHandlers>// 首先尝试从缓存中读取<httpHandlers>
  534. <httpHandlers>
  535.    
  536. </httpHandlers>
  537. </httpHandlers><httpHandlers>
  538. <httpHandlers>
  539.    
  540. </httpHandlers>
  541. </httpHandlers>string key = action + "@" + controller.FullName;<httpHandlers>
  542. <httpHandlers>
  543.    
  544. </httpHandlers>
  545. </httpHandlers><httpHandlers>
  546. <httpHandlers>
  547.    
  548. </httpHandlers>
  549. </httpHandlers>ActionDescription mi = (ActionDescription)s_AjaxActionTable[key];<httpHandlers>
  550. <httpHandlers>
  551.    
  552. </httpHandlers>
  553. </httpHandlers><httpHandlers>
  554. <httpHandlers>
  555.    
  556. </httpHandlers>
  557. </httpHandlers>if( mi == null ) {<httpHandlers>
  558. <httpHandlers>
  559.    
  560. </httpHandlers>
  561. </httpHandlers><httpHandlers>
  562. <httpHandlers>
  563.    
  564. </httpHandlers>
  565. </httpHandlers><httpHandlers>
  566. <httpHandlers>
  567.    
  568. </httpHandlers>
  569. </httpHandlers>// 注意:这里不考虑方法的重载。<httpHandlers>
  570. <httpHandlers>
  571.    
  572. </httpHandlers>
  573. </httpHandlers><httpHandlers>
  574. <httpHandlers>
  575.    
  576. </httpHandlers>
  577. </httpHandlers><httpHandlers>
  578. <httpHandlers>
  579.    
  580. </httpHandlers>
  581. </httpHandlers>MethodInfo method = controller.GetMethod(action, ActionBindingFlags);<httpHandlers>
  582. <httpHandlers>
  583.    
  584. </httpHandlers>
  585. </httpHandlers><httpHandlers>
  586. <httpHandlers>
  587.    
  588. </httpHandlers>
  589. </httpHandlers><httpHandlers>
  590. <httpHandlers>
  591.    
  592. </httpHandlers>
  593. </httpHandlers>if( method == null )<httpHandlers>
  594. <httpHandlers>
  595.    
  596. </httpHandlers>
  597. </httpHandlers><httpHandlers>
  598. <httpHandlers>
  599.    
  600. </httpHandlers>
  601. </httpHandlers><httpHandlers>
  602. <httpHandlers>
  603.    
  604. </httpHandlers>
  605. </httpHandlers><httpHandlers>
  606. <httpHandlers>
  607.    
  608. </httpHandlers>
  609. </httpHandlers>return null;<httpHandlers>
  610. <httpHandlers>
  611.    
  612. </httpHandlers>
  613. </httpHandlers><httpHandlers>
  614. <httpHandlers>
  615.    
  616. </httpHandlers>
  617. </httpHandlers><httpHandlers>
  618. <httpHandlers>
  619.    
  620. </httpHandlers>
  621. </httpHandlers>var attrs = (ActionAttribute[])method.GetCustomAttributes(typeof(ActionAttribute), false);<httpHandlers>
  622. <httpHandlers>
  623.    
  624. </httpHandlers>
  625. </httpHandlers><httpHandlers>
  626. <httpHandlers>
  627.    
  628. </httpHandlers>
  629. </httpHandlers><httpHandlers>
  630. <httpHandlers>
  631.    
  632. </httpHandlers>
  633. </httpHandlers>if( attrs.Length != 1 )<httpHandlers>
  634. <httpHandlers>
  635.    
  636. </httpHandlers>
  637. </httpHandlers><httpHandlers>
  638. <httpHandlers>
  639.    
  640. </httpHandlers>
  641. </httpHandlers><httpHandlers>
  642. <httpHandlers>
  643.    
  644. </httpHandlers>
  645. </httpHandlers><httpHandlers>
  646. <httpHandlers>
  647.    
  648. </httpHandlers>
  649. </httpHandlers>return null;<httpHandlers>
  650. <httpHandlers>
  651.    
  652. </httpHandlers>
  653. </httpHandlers><httpHandlers>
  654. <httpHandlers>
  655.    
  656. </httpHandlers>
  657. </httpHandlers><httpHandlers>
  658. <httpHandlers>
  659.    
  660. </httpHandlers>
  661. </httpHandlers><httpHandlers>
  662. <httpHandlers>
  663.    
  664. </httpHandlers>
  665. </httpHandlers><httpHandlers>
  666. <httpHandlers>
  667.    
  668. </httpHandlers>
  669. </httpHandlers><httpHandlers>
  670. <httpHandlers>
  671.    
  672. </httpHandlers>
  673. </httpHandlers>mi = new ActionDescription(method, attrs[0]);<httpHandlers>
  674. <httpHandlers>
  675.    
  676. </httpHandlers>
  677. </httpHandlers><httpHandlers>
  678. <httpHandlers>
  679.    
  680. </httpHandlers>
  681. </httpHandlers><httpHandlers>
  682. <httpHandlers>
  683.    
  684. </httpHandlers>
  685. </httpHandlers>s_AjaxActionTable[key] = mi;<httpHandlers>
  686. <httpHandlers>
  687.    
  688. </httpHandlers>
  689. </httpHandlers><httpHandlers>
  690. <httpHandlers>
  691.    
  692. </httpHandlers>
  693. </httpHandlers>}<httpHandlers>
  694. <httpHandlers>
  695.    
  696. </httpHandlers>
  697. </httpHandlers><httpHandlers>
  698. <httpHandlers>
  699.    
  700. </httpHandlers>
  701. </httpHandlers>return mi;<httpHandlers>
  702. <httpHandlers>
  703.    
  704. </httpHandlers>
  705. </httpHandlers>}<httpHandlers>
  706. <httpHandlers>
  707.    
  708. </httpHandlers>
  709. </httpHandlers>///<httpHandlers>
  710. <httpHandlers>
  711.    
  712. </httpHandlers>
  713. </httpHandlers> /// 根据一个AJAX的调用信息(类名与方法名),返回内部表示的调用信息。<httpHandlers>
  714. <httpHandlers>
  715.    
  716. </httpHandlers>
  717. </httpHandlers>///<httpHandlers>
  718. <httpHandlers>
  719.    
  720. </httpHandlers>
  721. </httpHandlers> ///<httpHandlers>
  722. <httpHandlers>
  723.    
  724. </httpHandlers>
  725. </httpHandlers> ///<httpHandlers>
  726. <httpHandlers>
  727.    
  728. </httpHandlers>
  729. </httpHandlers> public static InvokeInfo GetAjaxInvokeInfo(ControllerActionPair pair)<httpHandlers>
  730. <httpHandlers>
  731.    
  732. </httpHandlers>
  733. </httpHandlers>{<httpHandlers>
  734. <httpHandlers>
  735.    
  736. </httpHandlers>
  737. </httpHandlers><httpHandlers>
  738. <httpHandlers>
  739.    
  740. </httpHandlers>
  741. </httpHandlers>if( pair == null )<httpHandlers>
  742. <httpHandlers>
  743.    
  744. </httpHandlers>
  745. </httpHandlers><httpHandlers>
  746. <httpHandlers>
  747.    
  748. </httpHandlers>
  749. </httpHandlers><httpHandlers>
  750. <httpHandlers>
  751.    
  752. </httpHandlers>
  753. </httpHandlers>throw new ArgumentNullException("pair");<httpHandlers>
  754. <httpHandlers>
  755.    
  756. </httpHandlers>
  757. </httpHandlers><httpHandlers>
  758. <httpHandlers>
  759.    
  760. </httpHandlers>
  761. </httpHandlers>InvokeInfo vkInfo = new InvokeInfo();<httpHandlers>
  762. <httpHandlers>
  763.    
  764. </httpHandlers>
  765. </httpHandlers><httpHandlers>
  766. <httpHandlers>
  767.    
  768. </httpHandlers>
  769. </httpHandlers>vkInfo.Controller = GetAjaxController(pair.Controller);<httpHandlers>
  770. <httpHandlers>
  771.    
  772. </httpHandlers>
  773. </httpHandlers><httpHandlers>
  774. <httpHandlers>
  775.    
  776. </httpHandlers>
  777. </httpHandlers>if( vkInfo.Controller == null )<httpHandlers>
  778. <httpHandlers>
  779.    
  780. </httpHandlers>
  781. </httpHandlers><httpHandlers>
  782. <httpHandlers>
  783.    
  784. </httpHandlers>
  785. </httpHandlers><httpHandlers>
  786. <httpHandlers>
  787.    
  788. </httpHandlers>
  789. </httpHandlers>return null;<httpHandlers>
  790. <httpHandlers>
  791.    
  792. </httpHandlers>
  793. </httpHandlers><httpHandlers>
  794. <httpHandlers>
  795.    
  796. </httpHandlers>
  797. </httpHandlers>vkInfo.Action = GetAjaxAction(vkInfo.Controller.ControllerType, pair.Action);<httpHandlers>
  798. <httpHandlers>
  799.    
  800. </httpHandlers>
  801. </httpHandlers><httpHandlers>
  802. <httpHandlers>
  803.    
  804. </httpHandlers>
  805. </httpHandlers>if( vkInfo.Action == null )<httpHandlers>
  806. <httpHandlers>
  807.    
  808. </httpHandlers>
  809. </httpHandlers><httpHandlers>
  810. <httpHandlers>
  811.    
  812. </httpHandlers>
  813. </httpHandlers><httpHandlers>
  814. <httpHandlers>
  815.    
  816. </httpHandlers>
  817. </httpHandlers>return null;<httpHandlers>
  818. <httpHandlers>
  819.    
  820. </httpHandlers>
  821. </httpHandlers><httpHandlers>
  822. <httpHandlers>
  823.    
  824. </httpHandlers>
  825. </httpHandlers><httpHandlers>
  826. <httpHandlers>
  827.    
  828. </httpHandlers>
  829. </httpHandlers><httpHandlers>
  830. <httpHandlers>
  831.    
  832. </httpHandlers>
  833. </httpHandlers>if( vkInfo.Action.MethodInfo.IsStatic == false )<httpHandlers>
  834. <httpHandlers>
  835.    
  836. </httpHandlers>
  837. </httpHandlers><httpHandlers>
  838. <httpHandlers>
  839.    
  840. </httpHandlers>
  841. </httpHandlers><httpHandlers>
  842. <httpHandlers>
  843.    
  844. </httpHandlers>
  845. </httpHandlers>vkInfo.Instance = Activator.CreateInstance(vkInfo.Controller.ControllerType);<httpHandlers>
  846. <httpHandlers>
  847.    
  848. </httpHandlers>
  849. </httpHandlers><httpHandlers>
  850. <httpHandlers>
  851.    
  852. </httpHandlers>
  853. </httpHandlers><httpHandlers>
  854. <httpHandlers>
  855.    
  856. </httpHandlers>
  857. </httpHandlers><httpHandlers>
  858. <httpHandlers>
  859.    
  860. </httpHandlers>
  861. </httpHandlers>return vkInfo;<httpHandlers>
  862. <httpHandlers>
  863.    
  864. </httpHandlers>
  865. </httpHandlers>}
复制代码
上面就是AjaxAction查找相关的4段代码:
1. 在ReflectionHelper的静态构造函数中,我加载了所有AjaxController。
2. GetAjaxController方法用于根据一个Controller的名字返回Controller的类型描述。
3. GetAjaxAction方法用于根据Controller的类型以及要调用的Action的名字返回Action的描述信息。
4. GetAjaxInvokeInfo方法用于根据从AjaxHandlerFactory得到的ControllerActionPair描述转成更具体的描述信息。
代码中,Action的查找过程采用了延迟的加载模式,保存Action描述信息的集合我采用了线程安全的Hashtable
好了,上面那段代码我想说的就这些,剩下的就只些反射的使用,这也算是个基本功,而且也不是三言二语能说清楚的。因此,我打算继续谈其它的内容了。
MyMVC框架的实现原理 - 执行Action的过程

在AjaxHandlerFactory的GetHandler方法中,最后将创建一个ActionHandler,这是一个HttpHandler,它将在管线的第15个步骤中被调用(引用博客【用Asp.net写自己的服务框架】中的顺序)。
注意:AjaxHandlerFactory的GetHandler方法是在第10步中调用的,第12步就是在准备Session(非进程内模式),因此,必须在第12步前决定Session的使用方式。
所有的Action代码都是在ActionHandler中执行的:
  1. internal class ActionHandler : IHttpHandler{<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>internal InvokeInfo InvokeInfo;<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>public void ProcessRequest(HttpContext context)<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>{<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers><httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers>// 调用核心的工具类,执行Action<httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers><httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers>ActionExecutor.ExecuteAction(context, this.InvokeInfo);<httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers>}
复制代码
ExecuteAction的实现过程如下:
  1. internal static void ExecuteAction(HttpContext context, InvokeInfo vkInfo){<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>if( context == null )<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers><httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>throw new ArgumentNullException("context");<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>if( vkInfo == null )<httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers><httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers>throw new ArgumentNullException("vkInfo");<httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers>// 调用方法<httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers>object result = ExecuteActionInternal(context, vkInfo);<httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers>// 设置OutputCache<httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers>OutputCacheAttribute outputCache = vkInfo.GetOutputCacheSetting();<httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers>if( outputCache != null )<httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers><httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers>outputCache.SetResponseCache(context);<httpHandlers>
  54. <httpHandlers>
  55.    
  56. </httpHandlers>
  57. </httpHandlers>// 处理方法的返回结果<httpHandlers>
  58. <httpHandlers>
  59.    
  60. </httpHandlers>
  61. </httpHandlers>IActionResult executeResult = result as IActionResult;<httpHandlers>
  62. <httpHandlers>
  63.    
  64. </httpHandlers>
  65. </httpHandlers>if( executeResult != null ) {<httpHandlers>
  66. <httpHandlers>
  67.    
  68. </httpHandlers>
  69. </httpHandlers><httpHandlers>
  70. <httpHandlers>
  71.    
  72. </httpHandlers>
  73. </httpHandlers>executeResult.Ouput(context);<httpHandlers>
  74. <httpHandlers>
  75.    
  76. </httpHandlers>
  77. </httpHandlers>}<httpHandlers>
  78. <httpHandlers>
  79.    
  80. </httpHandlers>
  81. </httpHandlers>else {<httpHandlers>
  82. <httpHandlers>
  83.    
  84. </httpHandlers>
  85. </httpHandlers><httpHandlers>
  86. <httpHandlers>
  87.    
  88. </httpHandlers>
  89. </httpHandlers>if( result != null ) {<httpHandlers>
  90. <httpHandlers>
  91.    
  92. </httpHandlers>
  93. </httpHandlers><httpHandlers>
  94. <httpHandlers>
  95.    
  96. </httpHandlers>
  97. </httpHandlers><httpHandlers>
  98. <httpHandlers>
  99.    
  100. </httpHandlers>
  101. </httpHandlers>// 普通类型结果<httpHandlers>
  102. <httpHandlers>
  103.    
  104. </httpHandlers>
  105. </httpHandlers><httpHandlers>
  106. <httpHandlers>
  107.    
  108. </httpHandlers>
  109. </httpHandlers><httpHandlers>
  110. <httpHandlers>
  111.    
  112. </httpHandlers>
  113. </httpHandlers>context.Response.ContentType = "text/plain";<httpHandlers>
  114. <httpHandlers>
  115.    
  116. </httpHandlers>
  117. </httpHandlers><httpHandlers>
  118. <httpHandlers>
  119.    
  120. </httpHandlers>
  121. </httpHandlers><httpHandlers>
  122. <httpHandlers>
  123.    
  124. </httpHandlers>
  125. </httpHandlers>context.Response.Write(result.ToString());<httpHandlers>
  126. <httpHandlers>
  127.    
  128. </httpHandlers>
  129. </httpHandlers><httpHandlers>
  130. <httpHandlers>
  131.    
  132. </httpHandlers>
  133. </httpHandlers>}<httpHandlers>
  134. <httpHandlers>
  135.    
  136. </httpHandlers>
  137. </httpHandlers>}}internal static object ExecuteActionInternal(HttpContext context, InvokeInfo info){<httpHandlers>
  138. <httpHandlers>
  139.    
  140. </httpHandlers>
  141. </httpHandlers>// 准备要传给调用方法的参数<httpHandlers>
  142. <httpHandlers>
  143.    
  144. </httpHandlers>
  145. </httpHandlers>object[] parameters = GetActionCallParameters(context, info.Action);<httpHandlers>
  146. <httpHandlers>
  147.    
  148. </httpHandlers>
  149. </httpHandlers>// 调用方法<httpHandlers>
  150. <httpHandlers>
  151.    
  152. </httpHandlers>
  153. </httpHandlers>if( info.Action.HasReturn )<httpHandlers>
  154. <httpHandlers>
  155.    
  156. </httpHandlers>
  157. </httpHandlers><httpHandlers>
  158. <httpHandlers>
  159.    
  160. </httpHandlers>
  161. </httpHandlers>return info.Action.MethodInfo.Invoke(info.Instance, parameters);<httpHandlers>
  162. <httpHandlers>
  163.    
  164. </httpHandlers>
  165. </httpHandlers>else {<httpHandlers>
  166. <httpHandlers>
  167.    
  168. </httpHandlers>
  169. </httpHandlers><httpHandlers>
  170. <httpHandlers>
  171.    
  172. </httpHandlers>
  173. </httpHandlers>info.Action.MethodInfo.Invoke(info.Instance, parameters);<httpHandlers>
  174. <httpHandlers>
  175.    
  176. </httpHandlers>
  177. </httpHandlers><httpHandlers>
  178. <httpHandlers>
  179.    
  180. </httpHandlers>
  181. </httpHandlers>return null;<httpHandlers>
  182. <httpHandlers>
  183.    
  184. </httpHandlers>
  185. </httpHandlers>}}
复制代码
前面我不是没有说调用SetResponseCache()的时机嘛,这个时机就是在这里:执行完Action后。
设置过OutputCache后,就是处理返回值了。
前面那段代码中,还有一句重要的调用:
  1. // 准备要传给调用方法的参数
  2. object[] parameters = GetActionCallParameters(context, info.Action);
复制代码
这个调用的意义在注释中有解释,关于这个过程的实现方式还请继续阅读。
MyMVC框架的实现原理 - 如何给方法赋值

用过反射的人都知道,调用一个方法很简单,但如何给一个【不知签名】的方法准备传入参数呢?
下面就来回答这个问题,请接着看GetActionCallParameters的实现过程:
  1. private static object[] GetActionCallParameters(HttpContext context, ActionDescription action){<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>if( action.Parameters == null || action.Parameters.Length == 0 )<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers><httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>return null;<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>object[] parameters = new object[action.Parameters.Length];<httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers>for( int i = 0; i < action.Parameters.Length; i++ ) {<httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers><httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers>ParameterInfo p = action.Parameters[i];<httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers><httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers>if( p.IsOut )<httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers><httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers><httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers>continue;<httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers><httpHandlers>
  54. <httpHandlers>
  55.    
  56. </httpHandlers>
  57. </httpHandlers>if( p.ParameterType == typeof(NameValueCollection) ) {<httpHandlers>
  58. <httpHandlers>
  59.    
  60. </httpHandlers>
  61. </httpHandlers><httpHandlers>
  62. <httpHandlers>
  63.    
  64. </httpHandlers>
  65. </httpHandlers><httpHandlers>
  66. <httpHandlers>
  67.    
  68. </httpHandlers>
  69. </httpHandlers>if( string.Compare(p.Name, "Form", StringComparison.OrdinalIgnoreCase) == 0 )<httpHandlers>
  70. <httpHandlers>
  71.    
  72. </httpHandlers>
  73. </httpHandlers><httpHandlers>
  74. <httpHandlers>
  75.    
  76. </httpHandlers>
  77. </httpHandlers><httpHandlers>
  78. <httpHandlers>
  79.    
  80. </httpHandlers>
  81. </httpHandlers><httpHandlers>
  82. <httpHandlers>
  83.    
  84. </httpHandlers>
  85. </httpHandlers>parameters[i] = context.Request.Form;<httpHandlers>
  86. <httpHandlers>
  87.    
  88. </httpHandlers>
  89. </httpHandlers><httpHandlers>
  90. <httpHandlers>
  91.    
  92. </httpHandlers>
  93. </httpHandlers><httpHandlers>
  94. <httpHandlers>
  95.    
  96. </httpHandlers>
  97. </httpHandlers>else if( string.Compare(p.Name, "QueryString", StringComparison.OrdinalIgnoreCase) == 0 )<httpHandlers>
  98. <httpHandlers>
  99.    
  100. </httpHandlers>
  101. </httpHandlers><httpHandlers>
  102. <httpHandlers>
  103.    
  104. </httpHandlers>
  105. </httpHandlers><httpHandlers>
  106. <httpHandlers>
  107.    
  108. </httpHandlers>
  109. </httpHandlers><httpHandlers>
  110. <httpHandlers>
  111.    
  112. </httpHandlers>
  113. </httpHandlers>parameters[i] = context.Request.QueryString;<httpHandlers>
  114. <httpHandlers>
  115.    
  116. </httpHandlers>
  117. </httpHandlers><httpHandlers>
  118. <httpHandlers>
  119.    
  120. </httpHandlers>
  121. </httpHandlers><httpHandlers>
  122. <httpHandlers>
  123.    
  124. </httpHandlers>
  125. </httpHandlers>else if( string.Compare(p.Name, "Headers", StringComparison.OrdinalIgnoreCase) == 0 )<httpHandlers>
  126. <httpHandlers>
  127.    
  128. </httpHandlers>
  129. </httpHandlers><httpHandlers>
  130. <httpHandlers>
  131.    
  132. </httpHandlers>
  133. </httpHandlers><httpHandlers>
  134. <httpHandlers>
  135.    
  136. </httpHandlers>
  137. </httpHandlers><httpHandlers>
  138. <httpHandlers>
  139.    
  140. </httpHandlers>
  141. </httpHandlers>parameters[i] = context.Request.Headers;<httpHandlers>
  142. <httpHandlers>
  143.    
  144. </httpHandlers>
  145. </httpHandlers><httpHandlers>
  146. <httpHandlers>
  147.    
  148. </httpHandlers>
  149. </httpHandlers><httpHandlers>
  150. <httpHandlers>
  151.    
  152. </httpHandlers>
  153. </httpHandlers>else if( string.Compare(p.Name, "ServerVariables", StringComparison.OrdinalIgnoreCase) == 0 )<httpHandlers>
  154. <httpHandlers>
  155.    
  156. </httpHandlers>
  157. </httpHandlers><httpHandlers>
  158. <httpHandlers>
  159.    
  160. </httpHandlers>
  161. </httpHandlers><httpHandlers>
  162. <httpHandlers>
  163.    
  164. </httpHandlers>
  165. </httpHandlers><httpHandlers>
  166. <httpHandlers>
  167.    
  168. </httpHandlers>
  169. </httpHandlers>parameters[i] = context.Request.ServerVariables;<httpHandlers>
  170. <httpHandlers>
  171.    
  172. </httpHandlers>
  173. </httpHandlers><httpHandlers>
  174. <httpHandlers>
  175.    
  176. </httpHandlers>
  177. </httpHandlers>}<httpHandlers>
  178. <httpHandlers>
  179.    
  180. </httpHandlers>
  181. </httpHandlers><httpHandlers>
  182. <httpHandlers>
  183.    
  184. </httpHandlers>
  185. </httpHandlers>else{<httpHandlers>
  186. <httpHandlers>
  187.    
  188. </httpHandlers>
  189. </httpHandlers><httpHandlers>
  190. <httpHandlers>
  191.    
  192. </httpHandlers>
  193. </httpHandlers><httpHandlers>
  194. <httpHandlers>
  195.    
  196. </httpHandlers>
  197. </httpHandlers>Type paramterType = p.ParameterType.GetRealType();<httpHandlers>
  198. <httpHandlers>
  199.    
  200. </httpHandlers>
  201. </httpHandlers><httpHandlers>
  202. <httpHandlers>
  203.    
  204. </httpHandlers>
  205. </httpHandlers><httpHandlers>
  206. <httpHandlers>
  207.    
  208. </httpHandlers>
  209. </httpHandlers>// 如果参数是简单类型,则直接从HttpRequest中读取并赋值<httpHandlers>
  210. <httpHandlers>
  211.    
  212. </httpHandlers>
  213. </httpHandlers><httpHandlers>
  214. <httpHandlers>
  215.    
  216. </httpHandlers>
  217. </httpHandlers><httpHandlers>
  218. <httpHandlers>
  219.    
  220. </httpHandlers>
  221. </httpHandlers>if( paramterType.IsSimpleType() ) {<httpHandlers>
  222. <httpHandlers>
  223.    
  224. </httpHandlers>
  225. </httpHandlers><httpHandlers>
  226. <httpHandlers>
  227.    
  228. </httpHandlers>
  229. </httpHandlers><httpHandlers>
  230. <httpHandlers>
  231.    
  232. </httpHandlers>
  233. </httpHandlers><httpHandlers>
  234. <httpHandlers>
  235.    
  236. </httpHandlers>
  237. </httpHandlers>object val = ModelHelper.GetValueByKeyAndTypeFrommRequest(<httpHandlers>
  238. <httpHandlers>
  239.    
  240. </httpHandlers>
  241. </httpHandlers><httpHandlers>
  242. <httpHandlers>
  243.    
  244. </httpHandlers>
  245. </httpHandlers><httpHandlers>
  246. <httpHandlers>
  247.    
  248. </httpHandlers>
  249. </httpHandlers><httpHandlers>
  250. <httpHandlers>
  251.    
  252. </httpHandlers>
  253. </httpHandlers><httpHandlers>
  254. <httpHandlers>
  255.    
  256. </httpHandlers>
  257. </httpHandlers><httpHandlers>
  258. <httpHandlers>
  259.    
  260. </httpHandlers>
  261. </httpHandlers><httpHandlers>
  262. <httpHandlers>
  263.    
  264. </httpHandlers>
  265. </httpHandlers><httpHandlers>
  266. <httpHandlers>
  267.    
  268. </httpHandlers>
  269. </httpHandlers><httpHandlers>
  270. <httpHandlers>
  271.    
  272. </httpHandlers>
  273. </httpHandlers><httpHandlers>
  274. <httpHandlers>
  275.    
  276. </httpHandlers>
  277. </httpHandlers><httpHandlers>
  278. <httpHandlers>
  279.    
  280. </httpHandlers>
  281. </httpHandlers><httpHandlers>
  282. <httpHandlers>
  283.    
  284. </httpHandlers>
  285. </httpHandlers>context.Request, p.Name, paramterType, null);<httpHandlers>
  286. <httpHandlers>
  287.    
  288. </httpHandlers>
  289. </httpHandlers><httpHandlers>
  290. <httpHandlers>
  291.    
  292. </httpHandlers>
  293. </httpHandlers><httpHandlers>
  294. <httpHandlers>
  295.    
  296. </httpHandlers>
  297. </httpHandlers><httpHandlers>
  298. <httpHandlers>
  299.    
  300. </httpHandlers>
  301. </httpHandlers>if( val != null )<httpHandlers>
  302. <httpHandlers>
  303.    
  304. </httpHandlers>
  305. </httpHandlers><httpHandlers>
  306. <httpHandlers>
  307.    
  308. </httpHandlers>
  309. </httpHandlers><httpHandlers>
  310. <httpHandlers>
  311.    
  312. </httpHandlers>
  313. </httpHandlers><httpHandlers>
  314. <httpHandlers>
  315.    
  316. </httpHandlers>
  317. </httpHandlers><httpHandlers>
  318. <httpHandlers>
  319.    
  320. </httpHandlers>
  321. </httpHandlers>parameters[i] = val;<httpHandlers>
  322. <httpHandlers>
  323.    
  324. </httpHandlers>
  325. </httpHandlers><httpHandlers>
  326. <httpHandlers>
  327.    
  328. </httpHandlers>
  329. </httpHandlers><httpHandlers>
  330. <httpHandlers>
  331.    
  332. </httpHandlers>
  333. </httpHandlers>}<httpHandlers>
  334. <httpHandlers>
  335.    
  336. </httpHandlers>
  337. </httpHandlers><httpHandlers>
  338. <httpHandlers>
  339.    
  340. </httpHandlers>
  341. </httpHandlers><httpHandlers>
  342. <httpHandlers>
  343.    
  344. </httpHandlers>
  345. </httpHandlers>else {<httpHandlers>
  346. <httpHandlers>
  347.    
  348. </httpHandlers>
  349. </httpHandlers><httpHandlers>
  350. <httpHandlers>
  351.    
  352. </httpHandlers>
  353. </httpHandlers><httpHandlers>
  354. <httpHandlers>
  355.    
  356. </httpHandlers>
  357. </httpHandlers><httpHandlers>
  358. <httpHandlers>
  359.    
  360. </httpHandlers>
  361. </httpHandlers>// 自定义的类型。首先创建实例,然后给所有成员赋值。<httpHandlers>
  362. <httpHandlers>
  363.    
  364. </httpHandlers>
  365. </httpHandlers><httpHandlers>
  366. <httpHandlers>
  367.    
  368. </httpHandlers>
  369. </httpHandlers><httpHandlers>
  370. <httpHandlers>
  371.    
  372. </httpHandlers>
  373. </httpHandlers><httpHandlers>
  374. <httpHandlers>
  375.    
  376. </httpHandlers>
  377. </httpHandlers>// 注意:这里不支持嵌套类型的自定义类型。<httpHandlers>
  378. <httpHandlers>
  379.    
  380. </httpHandlers>
  381. </httpHandlers><httpHandlers>
  382. <httpHandlers>
  383.    
  384. </httpHandlers>
  385. </httpHandlers><httpHandlers>
  386. <httpHandlers>
  387.    
  388. </httpHandlers>
  389. </httpHandlers><httpHandlers>
  390. <httpHandlers>
  391.    
  392. </httpHandlers>
  393. </httpHandlers>object item = Activator.CreateInstance(paramterType);<httpHandlers>
  394. <httpHandlers>
  395.    
  396. </httpHandlers>
  397. </httpHandlers><httpHandlers>
  398. <httpHandlers>
  399.    
  400. </httpHandlers>
  401. </httpHandlers><httpHandlers>
  402. <httpHandlers>
  403.    
  404. </httpHandlers>
  405. </httpHandlers><httpHandlers>
  406. <httpHandlers>
  407.    
  408. </httpHandlers>
  409. </httpHandlers>ModelHelper.FillModel(context.Request, item, p.Name);<httpHandlers>
  410. <httpHandlers>
  411.    
  412. </httpHandlers>
  413. </httpHandlers><httpHandlers>
  414. <httpHandlers>
  415.    
  416. </httpHandlers>
  417. </httpHandlers><httpHandlers>
  418. <httpHandlers>
  419.    
  420. </httpHandlers>
  421. </httpHandlers><httpHandlers>
  422. <httpHandlers>
  423.    
  424. </httpHandlers>
  425. </httpHandlers>parameters[i] = item;<httpHandlers>
  426. <httpHandlers>
  427.    
  428. </httpHandlers>
  429. </httpHandlers><httpHandlers>
  430. <httpHandlers>
  431.    
  432. </httpHandlers>
  433. </httpHandlers><httpHandlers>
  434. <httpHandlers>
  435.    
  436. </httpHandlers>
  437. </httpHandlers>}<httpHandlers>
  438. <httpHandlers>
  439.    
  440. </httpHandlers>
  441. </httpHandlers><httpHandlers>
  442. <httpHandlers>
  443.    
  444. </httpHandlers>
  445. </httpHandlers>}<httpHandlers>
  446. <httpHandlers>
  447.    
  448. </httpHandlers>
  449. </httpHandlers>}<httpHandlers>
  450. <httpHandlers>
  451.    
  452. </httpHandlers>
  453. </httpHandlers>return parameters;}
复制代码
要理解这段代码还要从前面的【查找Action的过程】说起,在那个阶段,可以获取一个Action的描述,具体在框架内部表示为ActionDescription类型:
  1. internal sealed class ActionDescription : BaseDescription{<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>public ControllerDescription PageController; //为PageAction保留<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>public MethodInfo MethodInfo { get; private set; }<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>public ActionAttribute Attr { get; private set; }<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>public ParameterInfo[] Parameters { get; private set; }<httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers>public bool HasReturn { get; private set; }<httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers>public ActionDescription(MethodInfo m, ActionAttribute atrr) : base(m)<httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers>{<httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers><httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers>this.MethodInfo = m;<httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers><httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers>this.Attr = atrr;<httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers><httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers>this.Parameters = m.GetParameters();<httpHandlers>
  54. <httpHandlers>
  55.    
  56. </httpHandlers>
  57. </httpHandlers><httpHandlers>
  58. <httpHandlers>
  59.    
  60. </httpHandlers>
  61. </httpHandlers>this.HasReturn = m.ReturnType != ReflectionHelper.VoidType;<httpHandlers>
  62. <httpHandlers>
  63.    
  64. </httpHandlers>
  65. </httpHandlers>}}
复制代码
在构造函数的第三行代码中,我就可以得到这个方法的所有参数情况。
然后,我在就可以在GetActionCallParameters方法中,循环每个参数的定义,为它们赋值。
这段代码也解释了前面所说的只支持4种NameValueCollection集合的原因。
注意了,我在获取每个参数的类型时,是使用了下面的语句:
  1. Type paramterType = p.ParameterType.GetRealType();
复制代码
实际上,ParameterType就已经反映了参数的类型,为什么不直接使用它呢?
答:因为【可空泛型】的原因。这个类型我们需要特殊的处理。
例如:如果某个参数是这样声明的: int? id
那么,即使在QueryString中包含id这样一个参数,我也不能直接转成 int?  使用这种类型,必须得到它的【实际类型】。
GetRealType()是个扩展方法,它就专门完成这个功能:
  1. /// /// 得到一个实际的类型(排除Nullable类型的影响)。比如:int? 最后将得到int/// /// /// public static Type GetRealType(this Type type){<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>if( type.IsGenericType )<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers><httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>return Nullable.GetUnderlyingType(type) ?? type;<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>else<httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers><httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers>return type;}
复制代码
如果某个参数的类型是一个自定义的类型,框架会先创建实例(调用无参的构造函数),然后给它的Property, Field赋值。
注意了:自定义的类型,一定要提供一个无参的构造函数。
为自定义类型的实例填充数据成员的代码如下:
  1. internal static class ModelHelper{<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>public static readonly bool IsDebugMode;<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>static ModelHelper()<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>{<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers><httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers>CompilationSection configSection =<httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers><httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers><httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers><httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers><httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers> ConfigurationManager.GetSection("system.web/compilation") as CompilationSection;<httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers><httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers>if( configSection != null )<httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers><httpHandlers>
  54. <httpHandlers>
  55.    
  56. </httpHandlers>
  57. </httpHandlers><httpHandlers>
  58. <httpHandlers>
  59.    
  60. </httpHandlers>
  61. </httpHandlers>IsDebugMode = configSection.Debug;<httpHandlers>
  62. <httpHandlers>
  63.    
  64. </httpHandlers>
  65. </httpHandlers>}<httpHandlers>
  66. <httpHandlers>
  67.    
  68. </httpHandlers>
  69. </httpHandlers>///<httpHandlers>
  70. <httpHandlers>
  71.    
  72. </httpHandlers>
  73. </httpHandlers> /// 根据HttpRequest填充一个数据实体。<httpHandlers>
  74. <httpHandlers>
  75.    
  76. </httpHandlers>
  77. </httpHandlers>/// 这里不支持嵌套类型的数据实体,且要求各数据成员都是简单的数据类型。<httpHandlers>
  78. <httpHandlers>
  79.    
  80. </httpHandlers>
  81. </httpHandlers>///<httpHandlers>
  82. <httpHandlers>
  83.    
  84. </httpHandlers>
  85. </httpHandlers> ///<httpHandlers>
  86. <httpHandlers>
  87.    
  88. </httpHandlers>
  89. </httpHandlers> ///<httpHandlers>
  90. <httpHandlers>
  91.    
  92. </httpHandlers>
  93. </httpHandlers> public static void FillModel(HttpRequest request, object model, string paramName)<httpHandlers>
  94. <httpHandlers>
  95.    
  96. </httpHandlers>
  97. </httpHandlers>{<httpHandlers>
  98. <httpHandlers>
  99.    
  100. </httpHandlers>
  101. </httpHandlers><httpHandlers>
  102. <httpHandlers>
  103.    
  104. </httpHandlers>
  105. </httpHandlers>ModelDescripton descripton = ReflectionHelper.GetModelDescripton(model.GetType());<httpHandlers>
  106. <httpHandlers>
  107.    
  108. </httpHandlers>
  109. </httpHandlers><httpHandlers>
  110. <httpHandlers>
  111.    
  112. </httpHandlers>
  113. </httpHandlers>object val = null;<httpHandlers>
  114. <httpHandlers>
  115.    
  116. </httpHandlers>
  117. </httpHandlers><httpHandlers>
  118. <httpHandlers>
  119.    
  120. </httpHandlers>
  121. </httpHandlers>foreach( DataMember field in descripton.Fields ) {<httpHandlers>
  122. <httpHandlers>
  123.    
  124. </httpHandlers>
  125. </httpHandlers><httpHandlers>
  126. <httpHandlers>
  127.    
  128. </httpHandlers>
  129. </httpHandlers><httpHandlers>
  130. <httpHandlers>
  131.    
  132. </httpHandlers>
  133. </httpHandlers>// 这里的实现方式不支持嵌套类型的数据实体。<httpHandlers>
  134. <httpHandlers>
  135.    
  136. </httpHandlers>
  137. </httpHandlers><httpHandlers>
  138. <httpHandlers>
  139.    
  140. </httpHandlers>
  141. </httpHandlers><httpHandlers>
  142. <httpHandlers>
  143.    
  144. </httpHandlers>
  145. </httpHandlers>// 如果有这方面的需求,可以将这里改成递归的嵌套调用。<httpHandlers>
  146. <httpHandlers>
  147.    
  148. </httpHandlers>
  149. </httpHandlers><httpHandlers>
  150. <httpHandlers>
  151.    
  152. </httpHandlers>
  153. </httpHandlers><httpHandlers>
  154. <httpHandlers>
  155.    
  156. </httpHandlers>
  157. </httpHandlers>val = GetValueByKeyAndTypeFrommRequest(<httpHandlers>
  158. <httpHandlers>
  159.    
  160. </httpHandlers>
  161. </httpHandlers><httpHandlers>
  162. <httpHandlers>
  163.    
  164. </httpHandlers>
  165. </httpHandlers><httpHandlers>
  166. <httpHandlers>
  167.    
  168. </httpHandlers>
  169. </httpHandlers><httpHandlers>
  170. <httpHandlers>
  171.    
  172. </httpHandlers>
  173. </httpHandlers><httpHandlers>
  174. <httpHandlers>
  175.    
  176. </httpHandlers>
  177. </httpHandlers><httpHandlers>
  178. <httpHandlers>
  179.    
  180. </httpHandlers>
  181. </httpHandlers><httpHandlers>
  182. <httpHandlers>
  183.    
  184. </httpHandlers>
  185. </httpHandlers><httpHandlers>
  186. <httpHandlers>
  187.    
  188. </httpHandlers>
  189. </httpHandlers>request, field.Name, field.Type.GetRealType(), paramName);<httpHandlers>
  190. <httpHandlers>
  191.    
  192. </httpHandlers>
  193. </httpHandlers><httpHandlers>
  194. <httpHandlers>
  195.    
  196. </httpHandlers>
  197. </httpHandlers><httpHandlers>
  198. <httpHandlers>
  199.    
  200. </httpHandlers>
  201. </httpHandlers>if( val != null )<httpHandlers>
  202. <httpHandlers>
  203.    
  204. </httpHandlers>
  205. </httpHandlers><httpHandlers>
  206. <httpHandlers>
  207.    
  208. </httpHandlers>
  209. </httpHandlers><httpHandlers>
  210. <httpHandlers>
  211.    
  212. </httpHandlers>
  213. </httpHandlers><httpHandlers>
  214. <httpHandlers>
  215.    
  216. </httpHandlers>
  217. </httpHandlers>field.SetValue(model, val);<httpHandlers>
  218. <httpHandlers>
  219.    
  220. </httpHandlers>
  221. </httpHandlers><httpHandlers>
  222. <httpHandlers>
  223.    
  224. </httpHandlers>
  225. </httpHandlers>}<httpHandlers>
  226. <httpHandlers>
  227.    
  228. </httpHandlers>
  229. </httpHandlers>}<httpHandlers>
  230. <httpHandlers>
  231.    
  232. </httpHandlers>
  233. </httpHandlers>///<httpHandlers>
  234. <httpHandlers>
  235.    
  236. </httpHandlers>
  237. </httpHandlers> /// 读取一个HTTP参数值。这里只读取QueryString以及Form<httpHandlers>
  238. <httpHandlers>
  239.    
  240. </httpHandlers>
  241. </httpHandlers>///<httpHandlers>
  242. <httpHandlers>
  243.    
  244. </httpHandlers>
  245. </httpHandlers> ///<httpHandlers>
  246. <httpHandlers>
  247.    
  248. </httpHandlers>
  249. </httpHandlers> ///<httpHandlers>
  250. <httpHandlers>
  251.    
  252. </httpHandlers>
  253. </httpHandlers> ///<httpHandlers>
  254. <httpHandlers>
  255.    
  256. </httpHandlers>
  257. </httpHandlers> public static string GetHttpValue(HttpRequest request, string key)<httpHandlers>
  258. <httpHandlers>
  259.    
  260. </httpHandlers>
  261. </httpHandlers>{<httpHandlers>
  262. <httpHandlers>
  263.    
  264. </httpHandlers>
  265. </httpHandlers><httpHandlers>
  266. <httpHandlers>
  267.    
  268. </httpHandlers>
  269. </httpHandlers>string val = request.QueryString[key];<httpHandlers>
  270. <httpHandlers>
  271.    
  272. </httpHandlers>
  273. </httpHandlers><httpHandlers>
  274. <httpHandlers>
  275.    
  276. </httpHandlers>
  277. </httpHandlers>if( val == null )<httpHandlers>
  278. <httpHandlers>
  279.    
  280. </httpHandlers>
  281. </httpHandlers><httpHandlers>
  282. <httpHandlers>
  283.    
  284. </httpHandlers>
  285. </httpHandlers><httpHandlers>
  286. <httpHandlers>
  287.    
  288. </httpHandlers>
  289. </httpHandlers>val = request.Form[key];<httpHandlers>
  290. <httpHandlers>
  291.    
  292. </httpHandlers>
  293. </httpHandlers><httpHandlers>
  294. <httpHandlers>
  295.    
  296. </httpHandlers>
  297. </httpHandlers>return val;<httpHandlers>
  298. <httpHandlers>
  299.    
  300. </httpHandlers>
  301. </httpHandlers>}<httpHandlers>
  302. <httpHandlers>
  303.    
  304. </httpHandlers>
  305. </httpHandlers><httpHandlers>
  306. <httpHandlers>
  307.    
  308. </httpHandlers>
  309. </httpHandlers>public static object GetValueByKeyAndTypeFrommRequest(<httpHandlers>
  310. <httpHandlers>
  311.    
  312. </httpHandlers>
  313. </httpHandlers><httpHandlers>
  314. <httpHandlers>
  315.    
  316. </httpHandlers>
  317. </httpHandlers><httpHandlers>
  318. <httpHandlers>
  319.    
  320. </httpHandlers>
  321. </httpHandlers><httpHandlers>
  322. <httpHandlers>
  323.    
  324. </httpHandlers>
  325. </httpHandlers><httpHandlers>
  326. <httpHandlers>
  327.    
  328. </httpHandlers>
  329. </httpHandlers><httpHandlers>
  330. <httpHandlers>
  331.    
  332. </httpHandlers>
  333. </httpHandlers>HttpRequest request, string key, Type type, string paramName)<httpHandlers>
  334. <httpHandlers>
  335.    
  336. </httpHandlers>
  337. </httpHandlers>{<httpHandlers>
  338. <httpHandlers>
  339.    
  340. </httpHandlers>
  341. </httpHandlers><httpHandlers>
  342. <httpHandlers>
  343.    
  344. </httpHandlers>
  345. </httpHandlers>// 不支持复杂类型<httpHandlers>
  346. <httpHandlers>
  347.    
  348. </httpHandlers>
  349. </httpHandlers><httpHandlers>
  350. <httpHandlers>
  351.    
  352. </httpHandlers>
  353. </httpHandlers>if( type.IsSimpleType() == false )<httpHandlers>
  354. <httpHandlers>
  355.    
  356. </httpHandlers>
  357. </httpHandlers><httpHandlers>
  358. <httpHandlers>
  359.    
  360. </httpHandlers>
  361. </httpHandlers><httpHandlers>
  362. <httpHandlers>
  363.    
  364. </httpHandlers>
  365. </httpHandlers>return null;<httpHandlers>
  366. <httpHandlers>
  367.    
  368. </httpHandlers>
  369. </httpHandlers><httpHandlers>
  370. <httpHandlers>
  371.    
  372. </httpHandlers>
  373. </httpHandlers>string val = GetHttpValue(request, key);<httpHandlers>
  374. <httpHandlers>
  375.    
  376. </httpHandlers>
  377. </httpHandlers><httpHandlers>
  378. <httpHandlers>
  379.    
  380. </httpHandlers>
  381. </httpHandlers>if( val == null ) {<httpHandlers>
  382. <httpHandlers>
  383.    
  384. </httpHandlers>
  385. </httpHandlers><httpHandlers>
  386. <httpHandlers>
  387.    
  388. </httpHandlers>
  389. </httpHandlers><httpHandlers>
  390. <httpHandlers>
  391.    
  392. </httpHandlers>
  393. </httpHandlers>// 再试一次。有可能是多个自定义类型,Form表单元素采用变量名做为前缀。<httpHandlers>
  394. <httpHandlers>
  395.    
  396. </httpHandlers>
  397. </httpHandlers><httpHandlers>
  398. <httpHandlers>
  399.    
  400. </httpHandlers>
  401. </httpHandlers><httpHandlers>
  402. <httpHandlers>
  403.    
  404. </httpHandlers>
  405. </httpHandlers>if( string.IsNullOrEmpty(paramName) == false ) {<httpHandlers>
  406. <httpHandlers>
  407.    
  408. </httpHandlers>
  409. </httpHandlers><httpHandlers>
  410. <httpHandlers>
  411.    
  412. </httpHandlers>
  413. </httpHandlers><httpHandlers>
  414. <httpHandlers>
  415.    
  416. </httpHandlers>
  417. </httpHandlers><httpHandlers>
  418. <httpHandlers>
  419.    
  420. </httpHandlers>
  421. </httpHandlers>val = GetHttpValue(request, paramName + "." + key);<httpHandlers>
  422. <httpHandlers>
  423.    
  424. </httpHandlers>
  425. </httpHandlers><httpHandlers>
  426. <httpHandlers>
  427.    
  428. </httpHandlers>
  429. </httpHandlers><httpHandlers>
  430. <httpHandlers>
  431.    
  432. </httpHandlers>
  433. </httpHandlers>}<httpHandlers>
  434. <httpHandlers>
  435.    
  436. </httpHandlers>
  437. </httpHandlers><httpHandlers>
  438. <httpHandlers>
  439.    
  440. </httpHandlers>
  441. </httpHandlers><httpHandlers>
  442. <httpHandlers>
  443.    
  444. </httpHandlers>
  445. </httpHandlers>if( val == null )<httpHandlers>
  446. <httpHandlers>
  447.    
  448. </httpHandlers>
  449. </httpHandlers><httpHandlers>
  450. <httpHandlers>
  451.    
  452. </httpHandlers>
  453. </httpHandlers><httpHandlers>
  454. <httpHandlers>
  455.    
  456. </httpHandlers>
  457. </httpHandlers><httpHandlers>
  458. <httpHandlers>
  459.    
  460. </httpHandlers>
  461. </httpHandlers>return null;<httpHandlers>
  462. <httpHandlers>
  463.    
  464. </httpHandlers>
  465. </httpHandlers><httpHandlers>
  466. <httpHandlers>
  467.    
  468. </httpHandlers>
  469. </httpHandlers>}<httpHandlers>
  470. <httpHandlers>
  471.    
  472. </httpHandlers>
  473. </httpHandlers><httpHandlers>
  474. <httpHandlers>
  475.    
  476. </httpHandlers>
  477. </httpHandlers>return SafeChangeType(val.Trim(), type);<httpHandlers>
  478. <httpHandlers>
  479.    
  480. </httpHandlers>
  481. </httpHandlers>}<httpHandlers>
  482. <httpHandlers>
  483.    
  484. </httpHandlers>
  485. </httpHandlers>public static object SafeChangeType(string value, Type conversionType)<httpHandlers>
  486. <httpHandlers>
  487.    
  488. </httpHandlers>
  489. </httpHandlers>{<httpHandlers>
  490. <httpHandlers>
  491.    
  492. </httpHandlers>
  493. </httpHandlers><httpHandlers>
  494. <httpHandlers>
  495.    
  496. </httpHandlers>
  497. </httpHandlers>if( conversionType == typeof(string) )<httpHandlers>
  498. <httpHandlers>
  499.    
  500. </httpHandlers>
  501. </httpHandlers><httpHandlers>
  502. <httpHandlers>
  503.    
  504. </httpHandlers>
  505. </httpHandlers><httpHandlers>
  506. <httpHandlers>
  507.    
  508. </httpHandlers>
  509. </httpHandlers>return value;<httpHandlers>
  510. <httpHandlers>
  511.    
  512. </httpHandlers>
  513. </httpHandlers><httpHandlers>
  514. <httpHandlers>
  515.    
  516. </httpHandlers>
  517. </httpHandlers>if( value == null || value.Length == 0 )<httpHandlers>
  518. <httpHandlers>
  519.    
  520. </httpHandlers>
  521. </httpHandlers><httpHandlers>
  522. <httpHandlers>
  523.    
  524. </httpHandlers>
  525. </httpHandlers><httpHandlers>
  526. <httpHandlers>
  527.    
  528. </httpHandlers>
  529. </httpHandlers>// 空字符串根本不能做任何转换,所以直接返回null<httpHandlers>
  530. <httpHandlers>
  531.    
  532. </httpHandlers>
  533. </httpHandlers><httpHandlers>
  534. <httpHandlers>
  535.    
  536. </httpHandlers>
  537. </httpHandlers><httpHandlers>
  538. <httpHandlers>
  539.    
  540. </httpHandlers>
  541. </httpHandlers>return null;<httpHandlers>
  542. <httpHandlers>
  543.    
  544. </httpHandlers>
  545. </httpHandlers><httpHandlers>
  546. <httpHandlers>
  547.    
  548. </httpHandlers>
  549. </httpHandlers>try {<httpHandlers>
  550. <httpHandlers>
  551.    
  552. </httpHandlers>
  553. </httpHandlers><httpHandlers>
  554. <httpHandlers>
  555.    
  556. </httpHandlers>
  557. </httpHandlers><httpHandlers>
  558. <httpHandlers>
  559.    
  560. </httpHandlers>
  561. </httpHandlers>// 为了简单,直接调用 .net framework中的方法。<httpHandlers>
  562. <httpHandlers>
  563.    
  564. </httpHandlers>
  565. </httpHandlers><httpHandlers>
  566. <httpHandlers>
  567.    
  568. </httpHandlers>
  569. </httpHandlers><httpHandlers>
  570. <httpHandlers>
  571.    
  572. </httpHandlers>
  573. </httpHandlers>// 如果转换失败,则会抛出异常。<httpHandlers>
  574. <httpHandlers>
  575.    
  576. </httpHandlers>
  577. </httpHandlers><httpHandlers>
  578. <httpHandlers>
  579.    
  580. </httpHandlers>
  581. </httpHandlers><httpHandlers>
  582. <httpHandlers>
  583.    
  584. </httpHandlers>
  585. </httpHandlers>return Convert.ChangeType(value, conversionType);<httpHandlers>
  586. <httpHandlers>
  587.    
  588. </httpHandlers>
  589. </httpHandlers><httpHandlers>
  590. <httpHandlers>
  591.    
  592. </httpHandlers>
  593. </httpHandlers>}<httpHandlers>
  594. <httpHandlers>
  595.    
  596. </httpHandlers>
  597. </httpHandlers><httpHandlers>
  598. <httpHandlers>
  599.    
  600. </httpHandlers>
  601. </httpHandlers>catch {<httpHandlers>
  602. <httpHandlers>
  603.    
  604. </httpHandlers>
  605. </httpHandlers><httpHandlers>
  606. <httpHandlers>
  607.    
  608. </httpHandlers>
  609. </httpHandlers><httpHandlers>
  610. <httpHandlers>
  611.    
  612. </httpHandlers>
  613. </httpHandlers>if( IsDebugMode )<httpHandlers>
  614. <httpHandlers>
  615.    
  616. </httpHandlers>
  617. </httpHandlers><httpHandlers>
  618. <httpHandlers>
  619.    
  620. </httpHandlers>
  621. </httpHandlers><httpHandlers>
  622. <httpHandlers>
  623.    
  624. </httpHandlers>
  625. </httpHandlers><httpHandlers>
  626. <httpHandlers>
  627.    
  628. </httpHandlers>
  629. </httpHandlers>throw;<httpHandlers>
  630. <httpHandlers>
  631.    
  632. </httpHandlers>
  633. </httpHandlers><httpHandlers>
  634. <httpHandlers>
  635.    
  636. </httpHandlers>
  637. </httpHandlers><httpHandlers>
  638. <httpHandlers>
  639.    
  640. </httpHandlers>
  641. </httpHandlers>// Debug 模式下抛异常<httpHandlers>
  642. <httpHandlers>
  643.    
  644. </httpHandlers>
  645. </httpHandlers><httpHandlers>
  646. <httpHandlers>
  647.    
  648. </httpHandlers>
  649. </httpHandlers><httpHandlers>
  650. <httpHandlers>
  651.    
  652. </httpHandlers>
  653. </httpHandlers>else<httpHandlers>
  654. <httpHandlers>
  655.    
  656. </httpHandlers>
  657. </httpHandlers><httpHandlers>
  658. <httpHandlers>
  659.    
  660. </httpHandlers>
  661. </httpHandlers><httpHandlers>
  662. <httpHandlers>
  663.    
  664. </httpHandlers>
  665. </httpHandlers><httpHandlers>
  666. <httpHandlers>
  667.    
  668. </httpHandlers>
  669. </httpHandlers>return null;<httpHandlers>
  670. <httpHandlers>
  671.    
  672. </httpHandlers>
  673. </httpHandlers>// Release模式下忽略异常(防止恶意用户错误输入)<httpHandlers>
  674. <httpHandlers>
  675.    
  676. </httpHandlers>
  677. </httpHandlers><httpHandlers>
  678. <httpHandlers>
  679.    
  680. </httpHandlers>
  681. </httpHandlers>}<httpHandlers>
  682. <httpHandlers>
  683.    
  684. </httpHandlers>
  685. </httpHandlers>}}
复制代码
在给自定义的数据类型实例加载数据前,需要先知道这个实例对象有哪些属性以及字段,这个过程的代码如下:
  1. /// /// 返回一个实体类型的描述信息(全部属性及字段)。/// /// /// public static ModelDescripton GetModelDescripton(Type type){<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>if( type == null )<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers><httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>throw new ArgumentNullException("type");<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers><httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers>string key = type.FullName;<httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers>ModelDescripton mm = (ModelDescripton)s_modelTable[key];<httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers>if( mm == null ) {<httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers><httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers>List list = new List();<httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers><httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers>(from p in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)<httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers><httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers> select new PropertyMember(p)).ToList().ForEach(x=>list.Add(x));<httpHandlers>
  54. <httpHandlers>
  55.    
  56. </httpHandlers>
  57. </httpHandlers><httpHandlers>
  58. <httpHandlers>
  59.    
  60. </httpHandlers>
  61. </httpHandlers>(from f in type.GetFields(BindingFlags.Instance | BindingFlags.Public)<httpHandlers>
  62. <httpHandlers>
  63.    
  64. </httpHandlers>
  65. </httpHandlers><httpHandlers>
  66. <httpHandlers>
  67.    
  68. </httpHandlers>
  69. </httpHandlers> select new FieldMember(f)).ToList().ForEach(x => list.Add(x));<httpHandlers>
  70. <httpHandlers>
  71.    
  72. </httpHandlers>
  73. </httpHandlers><httpHandlers>
  74. <httpHandlers>
  75.    
  76. </httpHandlers>
  77. </httpHandlers>mm = new ModelDescripton { Fields = list.ToArray() };<httpHandlers>
  78. <httpHandlers>
  79.    
  80. </httpHandlers>
  81. </httpHandlers><httpHandlers>
  82. <httpHandlers>
  83.    
  84. </httpHandlers>
  85. </httpHandlers>s_modelTable[key] = mm;<httpHandlers>
  86. <httpHandlers>
  87.    
  88. </httpHandlers>
  89. </httpHandlers>}<httpHandlers>
  90. <httpHandlers>
  91.    
  92. </httpHandlers>
  93. </httpHandlers>return mm;}
复制代码
在拿到一个类型的所有属性以及字段的描述信息后,就可以通过循环的方式,根据这些数据成员的名字去QueryString,Form读取所需的数据了。
参数准备好了,前面的调用就应该没有问题了吧?
MyMVC框架的实现原理 - 处理返回值

MyMVC框架处理返回值的时机是在ExecuteAction方法中(前面有那段代码)。
这里只做个简单的补充说明。
我为Action的结果定义了一个接口:
  1. public interface IActionResult{<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>void Ouput(HttpContext context);}
复制代码
框架内实现了4种ActionResult:
  1. /// <summary>
  2. /// 表示一个用户控件结果(用户控件将由框架执行)
  3. /// </summary>
  4. public sealed class UcResult : IActionResult
  5. /// <summary>
  6. /// 表示一个重定向的结果
  7. /// </summary>
  8. public sealed class RedirectResult : IActionResult
  9. /// <summary>
  10. /// 一个Json对象结果
  11. /// </summary>
  12. public sealed class JsonResult : IActionResult
  13. /// <summary>
  14. /// 表示一个页面结果(页面将由框架执行)
  15. /// </summary>
  16. public sealed class PageResult : IActionResult
复制代码
要输出返回值的时候,不仅使用了IActionResult接口,我还使用下面这个调用:
  1. context.Response.Write(result.ToString());
复制代码
不要小看了ToString()的调用。
对于自定义的数据类型来说,可以用它来控制最终输出给客户端的是JSON或者是XML,或者是您自己定义的文本序列化格式(比如:特殊分隔符拼接而成),因此,它有足够的能力可以取代JsonResult类型,而且同样不影响Action的单元测试。
ToString()的强大原因在于它是个虚方法,可以被派生类重写。
所以,如果您只打算返回一个数据实体对象给客户端,那么既可以实现IActionResult接口,还可以重写ToString方法。
MyMVC框架的实现原理 - 如何返回HTML片段

AJAX调用中,虽然以返回数据居多,但有时也会要求返回一段HTML,毕竟拼HTML代码在服务端会容易些。
MyMVC提供UcResult类型,用来将一个用户控件的呈现结果做为HTML输出。当然了,您也可以创建一个Page,采用Page来输出HTML,那么就要用到PageResult类型了。它们的使用代码如下:
  1. [Action]public object ShowCustomerPicker(string searchWord, int? page){<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>CustomerSearchInfo info = new CustomerSearchInfo();<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>info.SearchWord = searchWord ?? string.Empty;<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>info.PageIndex = page.HasValue ? page.Value - 1 : 0;<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>info.PageSize = AppHelper.DefaultPageSize;<httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers>CustomerPickerModel data = new CustomerPickerModel();<httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers>data.SearchInfo = info;<httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers>data.List = BllFactory.GetCustomerBLL().GetList(info);<httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers>return new UcResult("/Controls/Style1/CustomerPicker.ascx", data);}
复制代码
由于我从来不用Page输出一段HTML,因此没有准备在Ajax中使用PageResult的示例。但是,它们的使用方法是一样,因为:PageResult和UcResult的构造函数有着一致的签名方式。
再来说说创建UcResult对象那行代码:传入二个参数,第一个参数表示用户控件的位置(View),第二个参数表示呈现用户控件所需的数据(Model)。至于这个地方为什么要设计二个参数,请关注我的后续博客,因为它涉及到MVC的核心思想,今天的博客不打算谈这个话题。
MyMVC框架的实现原理 - 多命名空间的支持

前面的示例代码都是演示了如何设计一个能供JS调用的Action,事实上,您也看到了,其实就是加了个[Action]的方法而已,没有其它的特别之处了。不过,在现实开发中,类型的名字可能会冲突。比如:.NET就引入了命名空间来处理这种冲突的类名。
MyMVC支持同名的Controller的名字吗?
答案是肯定的:支持。
例如,我有下面二个类型。注意它们的名字是相同的。
  1. namespace Fish.AA{<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>public class AjaxTest<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>{<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers><httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>[Action]<httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers><httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers>public int Add(int a, int b)<httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers><httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers>{<httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers><httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers><httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers>return a + b;<httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers><httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers>}<httpHandlers>
  54. <httpHandlers>
  55.    
  56. </httpHandlers>
  57. </httpHandlers>}}namespace Fish.BB{<httpHandlers>
  58. <httpHandlers>
  59.    
  60. </httpHandlers>
  61. </httpHandlers>public class AddInfo<httpHandlers>
  62. <httpHandlers>
  63.    
  64. </httpHandlers>
  65. </httpHandlers>{<httpHandlers>
  66. <httpHandlers>
  67.    
  68. </httpHandlers>
  69. </httpHandlers><httpHandlers>
  70. <httpHandlers>
  71.    
  72. </httpHandlers>
  73. </httpHandlers>public int A;<httpHandlers>
  74. <httpHandlers>
  75.    
  76. </httpHandlers>
  77. </httpHandlers><httpHandlers>
  78. <httpHandlers>
  79.    
  80. </httpHandlers>
  81. </httpHandlers>public int B;<httpHandlers>
  82. <httpHandlers>
  83.    
  84. </httpHandlers>
  85. </httpHandlers>}<httpHandlers>
  86. <httpHandlers>
  87.    
  88. </httpHandlers>
  89. </httpHandlers>public class AjaxTest<httpHandlers>
  90. <httpHandlers>
  91.    
  92. </httpHandlers>
  93. </httpHandlers>{<httpHandlers>
  94. <httpHandlers>
  95.    
  96. </httpHandlers>
  97. </httpHandlers><httpHandlers>
  98. <httpHandlers>
  99.    
  100. </httpHandlers>
  101. </httpHandlers>[Action]<httpHandlers>
  102. <httpHandlers>
  103.    
  104. </httpHandlers>
  105. </httpHandlers><httpHandlers>
  106. <httpHandlers>
  107.    
  108. </httpHandlers>
  109. </httpHandlers>public int Add(AddInfo info)<httpHandlers>
  110. <httpHandlers>
  111.    
  112. </httpHandlers>
  113. </httpHandlers><httpHandlers>
  114. <httpHandlers>
  115.    
  116. </httpHandlers>
  117. </httpHandlers>{<httpHandlers>
  118. <httpHandlers>
  119.    
  120. </httpHandlers>
  121. </httpHandlers><httpHandlers>
  122. <httpHandlers>
  123.    
  124. </httpHandlers>
  125. </httpHandlers><httpHandlers>
  126. <httpHandlers>
  127.    
  128. </httpHandlers>
  129. </httpHandlers>return info.A + info.B + 10;<httpHandlers>
  130. <httpHandlers>
  131.    
  132. </httpHandlers>
  133. </httpHandlers>// 故意写错。<httpHandlers>
  134. <httpHandlers>
  135.    
  136. </httpHandlers>
  137. </httpHandlers><httpHandlers>
  138. <httpHandlers>
  139.    
  140. </httpHandlers>
  141. </httpHandlers>}<httpHandlers>
  142. <httpHandlers>
  143.    
  144. </httpHandlers>
  145. </httpHandlers>}}
复制代码
这二个类型不仅同名,而且还包含了同名的方法。(事实上,方法的签名也可以完全一样。)
那么,对于这种情况,JS如何去调用它们呢?
为了回答这个问题,我特意准备了一个示例,HTML代码如下:
  1. <httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>Fish.AA.AjaxTest.Add<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers> +<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers> =<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers><httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers>
  22. <httpHandlers>
  23. <httpHandlers>
  24.    
  25. </httpHandlers>
  26. </httpHandlers>Fish.BB.AjaxTest.Add<httpHandlers>
  27. <httpHandlers>
  28.    
  29. </httpHandlers>
  30. </httpHandlers> +<httpHandlers>
  31. <httpHandlers>
  32.    
  33. </httpHandlers>
  34. </httpHandlers> =<httpHandlers>
  35. <httpHandlers>
  36.    
  37. </httpHandlers>
  38. </httpHandlers><httpHandlers>
  39. <httpHandlers>
  40.    
  41. </httpHandlers>
  42. </httpHandlers>
复制代码
客户端的JS代码如下:
  1. $(function(){<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>$("#btnAdd1").click(function(){<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers><httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>$.ajax({<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers><httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers><httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers>url: "/Fish.AA.AjaxTest/Add.cspx",<httpHandlers>
  26. <httpHandlers>
  27.    
  28. </httpHandlers>
  29. </httpHandlers><httpHandlers>
  30. <httpHandlers>
  31.    
  32. </httpHandlers>
  33. </httpHandlers><httpHandlers>
  34. <httpHandlers>
  35.    
  36. </httpHandlers>
  37. </httpHandlers>data: {a: $("#txtA1").val(), b: $("#txtB1").val()},<httpHandlers>
  38. <httpHandlers>
  39.    
  40. </httpHandlers>
  41. </httpHandlers><httpHandlers>
  42. <httpHandlers>
  43.    
  44. </httpHandlers>
  45. </httpHandlers><httpHandlers>
  46. <httpHandlers>
  47.    
  48. </httpHandlers>
  49. </httpHandlers>success: function(responseText){<httpHandlers>
  50. <httpHandlers>
  51.    
  52. </httpHandlers>
  53. </httpHandlers><httpHandlers>
  54. <httpHandlers>
  55.    
  56. </httpHandlers>
  57. </httpHandlers><httpHandlers>
  58. <httpHandlers>
  59.    
  60. </httpHandlers>
  61. </httpHandlers><httpHandlers>
  62. <httpHandlers>
  63.    
  64. </httpHandlers>
  65. </httpHandlers>$("#spanResult1").text(responseText);<httpHandlers>
  66. <httpHandlers>
  67.    
  68. </httpHandlers>
  69. </httpHandlers><httpHandlers>
  70. <httpHandlers>
  71.    
  72. </httpHandlers>
  73. </httpHandlers><httpHandlers>
  74. <httpHandlers>
  75.    
  76. </httpHandlers>
  77. </httpHandlers>}<httpHandlers>
  78. <httpHandlers>
  79.    
  80. </httpHandlers>
  81. </httpHandlers><httpHandlers>
  82. <httpHandlers>
  83.    
  84. </httpHandlers>
  85. </httpHandlers>});<httpHandlers>
  86. <httpHandlers>
  87.    
  88. </httpHandlers>
  89. </httpHandlers>});<httpHandlers>
  90. <httpHandlers>
  91.    
  92. </httpHandlers>
  93. </httpHandlers><httpHandlers>
  94. <httpHandlers>
  95.    
  96. </httpHandlers>
  97. </httpHandlers>$("#btnAdd2").click(function(){<httpHandlers>
  98. <httpHandlers>
  99.    
  100. </httpHandlers>
  101. </httpHandlers><httpHandlers>
  102. <httpHandlers>
  103.    
  104. </httpHandlers>
  105. </httpHandlers>$.ajax({<httpHandlers>
  106. <httpHandlers>
  107.    
  108. </httpHandlers>
  109. </httpHandlers><httpHandlers>
  110. <httpHandlers>
  111.    
  112. </httpHandlers>
  113. </httpHandlers><httpHandlers>
  114. <httpHandlers>
  115.    
  116. </httpHandlers>
  117. </httpHandlers>// 以下二个URL地址都是有效的。<httpHandlers>
  118. <httpHandlers>
  119.    
  120. </httpHandlers>
  121. </httpHandlers><httpHandlers>
  122. <httpHandlers>
  123.    
  124. </httpHandlers>
  125. </httpHandlers><httpHandlers>
  126. <httpHandlers>
  127.    
  128. </httpHandlers>
  129. </httpHandlers>//url: "/Fish.BB.AjaxTest.Add.cspx",<httpHandlers>
  130. <httpHandlers>
  131.    
  132. </httpHandlers>
  133. </httpHandlers><httpHandlers>
  134. <httpHandlers>
  135.    
  136. </httpHandlers>
  137. </httpHandlers><httpHandlers>
  138. <httpHandlers>
  139.    
  140. </httpHandlers>
  141. </httpHandlers>url: "/Fish/BB/AjaxTest/Add.cspx",<httpHandlers>
  142. <httpHandlers>
  143.    
  144. </httpHandlers>
  145. </httpHandlers><httpHandlers>
  146. <httpHandlers>
  147.    
  148. </httpHandlers>
  149. </httpHandlers><httpHandlers>
  150. <httpHandlers>
  151.    
  152. </httpHandlers>
  153. </httpHandlers>data: {a: $("#txtA2").val(), b: $("#txtB2").val()},<httpHandlers>
  154. <httpHandlers>
  155.    
  156. </httpHandlers>
  157. </httpHandlers><httpHandlers>
  158. <httpHandlers>
  159.    
  160. </httpHandlers>
  161. </httpHandlers><httpHandlers>
  162. <httpHandlers>
  163.    
  164. </httpHandlers>
  165. </httpHandlers>success: function(responseText){<httpHandlers>
  166. <httpHandlers>
  167.    
  168. </httpHandlers>
  169. </httpHandlers><httpHandlers>
  170. <httpHandlers>
  171.    
  172. </httpHandlers>
  173. </httpHandlers><httpHandlers>
  174. <httpHandlers>
  175.    
  176. </httpHandlers>
  177. </httpHandlers><httpHandlers>
  178. <httpHandlers>
  179.    
  180. </httpHandlers>
  181. </httpHandlers>$("#spanResult2").text(responseText);<httpHandlers>
  182. <httpHandlers>
  183.    
  184. </httpHandlers>
  185. </httpHandlers><httpHandlers>
  186. <httpHandlers>
  187.    
  188. </httpHandlers>
  189. </httpHandlers><httpHandlers>
  190. <httpHandlers>
  191.    
  192. </httpHandlers>
  193. </httpHandlers>}<httpHandlers>
  194. <httpHandlers>
  195.    
  196. </httpHandlers>
  197. </httpHandlers><httpHandlers>
  198. <httpHandlers>
  199.    
  200. </httpHandlers>
  201. </httpHandlers>});<httpHandlers>
  202. <httpHandlers>
  203.    
  204. </httpHandlers>
  205. </httpHandlers>});});
复制代码
最终的调用结果如下:
7.png

注意:下方的调用结果虽然是错误的,但表示调用的方法是正确的。
让我们再来回顾一下UrlParser类中定义的那个正则表达式吧:
  1. internal static readonly string AjaxUrlPattern<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>= @"/(?(\w[\./\w]*)?(?=Ajax)\w+)[/\.](?\w+)\.[a-zA-Z]+";
复制代码
它可以解析这些格式的URL:
  1. /*<httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>可以解析以下格式的URL:(前三个表示包含命名空间的)<httpHandlers>
  6. <httpHandlers>
  7.    
  8. </httpHandlers>
  9. </httpHandlers>/Fish.AA.AjaxTest/Add.cspx<httpHandlers>
  10. <httpHandlers>
  11.    
  12. </httpHandlers>
  13. </httpHandlers>/Fish.BB.AjaxTest.Add.cspx<httpHandlers>
  14. <httpHandlers>
  15.    
  16. </httpHandlers>
  17. </httpHandlers>/Fish/BB/AjaxTest/Add.cspx<httpHandlers>
  18. <httpHandlers>
  19.    
  20. </httpHandlers>
  21. </httpHandlers>/AjaxDemo/GetMd5.cspx<httpHandlers>
  22. <httpHandlers>
  23.    
  24. </httpHandlers>
  25. </httpHandlers>/AjaxDemo.GetMd5.cspx*/
复制代码
值得说明的是:这个正则表达式并没有限定用什么样的扩展名,而且也不限制URL中的查询字符串参数。
但是,就算它再强大,还需要在web.config中注册时,要保证匹配的URL能被传入,否则代码根本没有机会运行。
重温httpHandlers的注册:
  1. <httpHandlers>
  2. <httpHandlers>
  3.    
  4. </httpHandlers>
  5. </httpHandlers>
复制代码
感谢微软的天才设计,让我可以用通配符的方式写正则表达式。
关于反射的使用

反射。
我想有些人听到这个名字,首先想到的会是低性能,并积极地拒绝使用。
在那些人的心目中,反射就是低性能的代名词。
有趣的是,那些人可能在乐滋滋地用着ASP.NET MVC, WCF, EntryFramewok这类框架。
这里我要说明的是,我并没有说那些框架比较差,而是想说:
那些框架其实也在大量地使用反射,只是微软没有直接说出来而已。
我不知道那些不喜欢的反射的人,知道这些框架在大量使用反射时,会有什么样的想法。
其实想知道一个框架有没有在使用反射,有个简单的识别方法:
1. 它有没有序列化和反序列化。
2. 有没有把类名与方法写在字符串中。
3. 它是不是可以神奇地知道你的任何对象拥有哪些成员?
4. 有没有使用[Attribute]。您不会以为这个标记是给编译器看的吧?
WCF简直是把这些全用上了,而且是在大量使用,ASP.NET MVC,EntryFramewok也没少用!
在实现MyMVC的过程,我大量地使用了反射。
没办法,不用反射,我真的写不出来什么东西。
我认为:没有哪个框架可以不使用反射的。
不使用反射,就意味着:在事先就需要知道将调用哪些类型的哪些方法,这样哪来的灵活性?
反射还有另一个好处就是简化代码,许多类似的代码,就像前面【回忆以往AJAX的实现方式】中总结的那样。那些类似的代码差别在于:参数的名字不同,参数的类型不同,参数的个数不同,要调用的方法以及返回值不同。那些惊呼【非ASP.NET MVC框架不可】的人或许也是厌倦了这些重复劳动,然而,ASP.NET MVC解决这个问题的办法还是反射。
所以,不必害怕反射,它的确会影响性能。
但是,你可以保证你的其它代码都是性能很好吗?
我见过的低性能代码实在是太多了。
反射是会影响性能,但好消息是,它对性能的影响是可以优化的,因此,不同的写法,所表现出来的影响也是不一样的。不过,反射的优化也是个复杂的话题,我打算以后有机会再谈。
结束语

今天的博客演示了我的MVC框架对于AJAX的支持,也展示了在ASP.NET上开发一个框架的具体过程,虽然还未全部说完,但核心部分已经实现了,那就是:根据URL动态调用一个方法。先说AJAX的实现是因为,它是【无界面】的,无界面的东西通常会比较简单。
说到【无界面】又让我想到一些人把微软的ASP.NET MVC用于【无界面】的项目,还在信誓旦旦地说:此类型的项目非微软的ASP.NET MVC不可!
如何评价这些人呢?我只想说:你们还是小点声吧,小心遭人鄙视!
说到写框架,我想还是有必要再说说我写框架的原因:(引用我在博客【用Asp.net写自己的服务框架】中的原话)
自己写框架的好处不在于能将它做得多强大,多完美,而是从写框架的过程中,可以学到很多东西。
一个框架写完了,不在乎要给多少人使用,而是自己感觉有没有进步,这才是关键。

不管你信不信,那些喜欢说【非什么什么不可】的人,通常是从来不会写框架的。
MyMVC的介绍还未结束,下篇博客将会继续,下篇博客的重点在于UI部分的支持和实现,这也正是MVC思想存在的必要性,当然也可以反映出MVC框架的核心价值。
说到这里,我打算给下篇博客做个预告:
MyMVC框架的后半部分在设计上主要体现了MVC这三者的关系,在设计时主要遵循了Martin Fowler大叔的总结:从模型中分离表现和从视图中分离控制器。
最终MyMVC对于UI部分支持的结果是:多个URL可以映射到一个Action,一个Action可以将结果指定给多个View来输出。也就是说:请求与View是一种多对多的关系,而中间的Controller只是一个。至于Model的返回,可以由Controller根据运行的上下文条件给出不同的结果,同一个Model可以交给不同的View来显示,也可以返回不同的Model,分别交给不同的View来显示。

写博客真不容易,为了写这篇博客,我先要写MyMVC框架以及准备示例代码,再准备一些Visio图,最后是文字部分,总共花了整整二个星期。这还不包括前面二篇做为铺垫的博客:【细说 ASP.NET控制HTTP缓存】和【细说 HttpHandler 的映射过程】。但是,每当看到自己写的博客在博客园上拥有较高的【推荐数量】时,感觉宽慰了许多。但愿今天的博客能受欢迎。
感谢 Amy(黄敏)同学为本文所做的校对工作,她已帮我找了好多处文字上的错误。

获取MyMVC框架源代码及示例代码请点击此处进入下载页面
如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】按钮。
如果,您希望更容易地发现我的新博客,不妨点击一下右下角的【关注 Fish Li】。
因为,我的写作热情也离不开您的肯定支持。
感谢您的阅读,如果您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客,我是Fish Li 。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册