0
点赞
收藏
分享

微信扫一扫

测试ASP_NET 生命周期

JakietYu 2022-03-31 阅读 41


抽空看了一下博主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是如何创建对象的吗?]


源码下载

举报

相关推荐

0 条评论