测试ASP_NET 生命周期
抽空看了一下博主GodSpeed 的《ASP.NET應用程序生命周期趣談系列》的三篇文章,講解得不錯。尤其是第三篇文章 ASP.NET應用程序生命周期趣談(三) HttpModule 的后半段,讓我收獲頗多,順便自己也做了一個Demo 進行測試。
首先:我在Web.config 配置文件中,分別在 <httpHandlers> 和 <httpModules> 節點添加了自己的配置。
Web.config 代碼如下:
| <httpHandlers> |
| <remove verb="*" path="*.asmx"/> |
| <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> |
| <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> |
| <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/> |
| <add verb="*" path="*.aspx" type="Web_1.XG_Handler"/> |
| </httpHandlers> |
| <httpModules> |
| <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> |
| <add name="XG_Module" type="Web_1.XG_Module"/> |
| </httpModules> |
其次:我添加了一個XG_Module.cs 類文件:
讓XG_Module 類實現了IHttpModule 接口;
讓XG_Handler 類實現了IHttpHandler 接口;
XG_Module.cs 代碼如下:
| using System; |
| using System.Configuration; |
| using System.Web; |
| namespace Web_1 |
| { |
| /* |
| * 在Web.config中: <add name="XG_Module" type="Web_1.XG_Module"/> |
| */ |
| public class XG_Module:IHttpModule |
| { |
| #region IHttpModule 成員 |
| public void Dispose() |
| { } |
| public void Init(HttpApplication context) |
| { |
| System.Diagnostics.Debug.WriteLine("調式輸出:XG_Module 哈哈~~ "); |
| //this.Init(context); //錯誤 |
| } |
| #endregion |
| } |
| /* |
| * 在Web.config中:<add verb="*" path="*.aspx" type="Web_1.XG_Handler"/> |
| */ |
| public class XG_Handler : IHttpHandler |
| { |
| #region IHttpHandler 成員 |
| public bool IsReusable |
| { get { return true; } } |
| public void ProcessRequest(HttpContext context) |
| { |
| //獲得客戶端請求 |
| System.Diagnostics.Debug.WriteLine(context.Request.QueryString[0]); |
| //返回服務器響應 |
| context.Response.Write("輸出:XG_Handler 呵呵~~ "); |
| } |
| #endregion |
| } |
| } |
XG_Module 類和XG_Handler 類中的System.Diagnostics.Debug.WriteLine( ) 方法在預期的時間打印了調試信息。
最后:為了更加直觀的看到客戶端與服務器端的通訊,我再次編寫了一個客戶端頁面AJAX_test.html 使用AJAX 技術直接與服務器通訊,效果更佳。
AJAX_test.html 代碼如下:
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml"> |
| <head> |
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| <title>無標題文檔</title> |
| <script type="text/javascript"> |
| var xmlhttp; |
| //創建異步對象 |
| function initXmlHttp(){ |
| if(window.ActiveXObject){ //IE瀏覽器 |
| xmlhttp = new window.ActiveXObject("Microsoft.XMLHTTP"); |
| } |
| else if(window.XMLHttpRequest){ //非IE瀏覽器 |
| xmlhttp = new window.XMLHttpRequest(); |
| } |
| } |
| window.onload = initXmlHttp; |
| //發送異步請求 |
| function sendRequest(){ |
| xmlhttp.open("GET","AJAX_servers.aspx?myname=xg"); //傳參myname |
| //指定當readyState屬性改變時的事件處理句柄onreadystatechange |
| xmlhttp.onreadystatechange = funState; |
| xmlhttp.send(null); |
| } |
| //獲取異步結果 |
| function funState(){ |
| if( xmlhttp.readyState == 4) |
| { |
| if( xmlhttp.status == 200 || //status==200 表示成功! |
| xmlhttp.status == 0 ) //本機測試時,status可能為0。 |
| { |
| var re = xmlhttp.responseText; |
| //alert(re); |
| document.getElementById("divShow").innerHTML = re; |
| } |
| } |
| } |
| </script> |
| </head> |
| <body> |
| <button onclick="sendRequest();">發送</button> |
| <div id="divShow"></div> |
| </body> |
| </html> |
對于代碼,我就不多解釋了,注釋寫得很清楚。
附加:管道+上下文(Pipeline + Context)模式
ASP.NET 的運行時就可以看成是一個由若干HttpModule 組成的處理HTTP 請求的管道,ASP.NET 運行時管道的上下文對象是HttpContext ;
WCF 中Binding 就是一個由若干信道(Channel)組成的處理Message 的管道,而Binding 管道的上下文對象是BindingContext ;
相同的設計還體現在.NET Remoting、BizTalk、Unity(Unity 是微軟P&P推出的一個開源的IoC框架)等相關框架和產品的設計上。[引用來源:你知道Unity IoC Container是如何創建對象的嗎?]
本文轉自鋼鋼博客園博客,原文鏈接:http://www.cnblogs.com/xugang/archive/2010/07/14/1777333.html,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的测试ASP_NET 生命周期的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 号召,有兴趣做博客园自己的网络游戏的请举
- 下一篇: Spring Cloud构建微服务架构-