分享

HttpHandler与HttpModule讲解

 趋明 2012-03-19
HttpHandler对象必须实现IHttpHandler接口,在HttpApplication对象转交要求至HttpHandler对象时会调用此接口中的init函数,下面是一个HelloHandler类的程序。

 

  1. using System;  
  2. using System.Web;  
  3. using System.Web.SessionState;  
  4.   
  5. namespace c64.HttpHandler  
  6. {  
  7.     public class HelloHandler : IHttpHandler,IRequiresSessionState  
  8.     {         
  9.         #region IHttpHandler Members   
  10.   
  11.         public void ProcessRequest(HttpContext context)  
  12.         {  
  13.             context.Response.ContentType = "text/html";  
  14.             context.Response.Write("<html>");  
  15.             context.Response.Write("<body>");  
  16.             context.Response.Write("<B>Response By HelloHandler</B>");  
  17.             context.Response.Write("</body");  
  18.             context.Response.Write("</html>");  
  19.         }  
  20.   
  21.         public bool IsReusable  
  22.         {  
  23.             get  
  24.             {                 
  25.                 return true;  
  26.             }  
  27.         }  
  28.  
  29.         #endregion   
  30.     }  
  31. }  

挂载这个HttpHandler对象的方法有两种,一种是直接修改.NET Framework目录中的machine.config文件,这样做将本机上所有的asp.net程序挂载此HttpHandler对象,另一中方式是修改虚拟目录中的web.config文件,此方案只会影响该虚拟目录中的asp.net程序。此处采用第二种方式,请于web.config文件中加入下面的叙述。

<httpHandlers>

   <add verb="*" path=".ho" type="c64.HttpHandler.HelloHandler,HelloHanlder"/>

</httpHanlders>

path部分的设定是告知HttpApplication对象此HttpHandler对象时对应那一种文件扩展名,type设定的前段是类的名称与namespace,后段是Assembly文件名。除此之外,要让aspnet_isapi.dll接受.ho文件的要求,还得在IIS中设定虚拟目录的内容设定扩展名对应表。

 

 

关于Session

默认情况下,当HttpHandler对象被调用时Session对象并未初始化成可用状态,倘若HttpHandler对象须要使用到Session对象时,必须同时实现IRequiresSessionState接口,此接口只是识别用途,无须实现任何的方法,下面的程序实现了IRequiresSessionState接口的HelloHandler类源代码。

  1. using System;  
  2. using System.Web;  
  3. using System.Web.SessionState;  
  4.   
  5. namespace c64.HttpHandler  
  6. {  
  7.     public class HelloHandler : IHttpHandler,IRequiresSessionState  
  8.     {         
  9.         #region IHttpHandler Members   
  10.   
  11.         public void ProcessRequest(HttpContext context)  
  12.         {  
  13.             context.Response.ContentType = "text/html";  
  14.             context.Response.Write("<html>");  
  15.             context.Response.Write("<body>");  
  16.             context.Response.Write("<B>Response By HelloHandler</B>");  
  17.             string user = (string)context.Session["USER"];  
  18.             if(user != null && user.Length > 0)  
  19.                 context.Response.Write(user);  
  20.             context.Response.Write("</body");  
  21.             context.Response.Write("</html>");  
  22.         }  
  23.   
  24.         public bool IsReusable  
  25.         {  
  26.             get  
  27.             {                 
  28.                 return true;  
  29.             }  
  30.         }  
  31.  
  32.         #endregion   
  33.     }  
  34. }  


 

要测试这个程序,我们可以在WebForm上放置一个按钮,当用户按下后存放一个字符串到Session["USER"]中,接着要求.ho的文件就可以看到结果。

 

 

实现HttpModule
与HttpHandler对象的加载时机不同,HttpModule对象是在HttpApplication对象初始化时就一并载入的,这个时机是在本机开机后第一个用户访问此虚拟目录时。设计人员撰写HttpModule对象的机会不多,甚至比撰写HttpHandler对象的机会还少。下面撰写一个HelloModule对象来示范如何撰写HttpModule对象。

 

  1. using System;  
  2. using System.Web;  
  3. using System.Web.SessionState;  
  4.   
  5. namespace HttpModule  
  6. {  
  7.     public class HelloModule : IHttpModule,IRequiresSessionState  
  8.     {         
  9.         #region IHttpModule Members   
  10.   
  11.         public void Init(HttpApplication context)  
  12.         {  
  13.             context.Application["HELLO"] = "I am Hello Module!";  
  14.             context.Application[GetHashCode().ToString()] = 123;  
  15.         }  
  16.   
  17.         public void Dispose()  
  18.         {             
  19.         }    
  20.  
  21.         #endregion   
  22.     }  
  23. }  

这个HelloModule对象只是很简单地在Application的State对象中放入一个字符串,与加载HttpHandler对象的方式相同,我们可以修改mechine.config文件或是虚拟目录内的web.config文件来载入它,此处采用第二种方案,请于web.config文件中放入以下的叙述。

<httpModules>

   <add name ="HelloModule"  type="HttpModule.HttpModule,HelloModule"/>

</httpModules>

要测试这个HelloModule对象,只须与WebForm上放置一个按钮,当用户按下后由Application State对象中取出由HelloModule对象所放入的字符串即可,下图是HelloModule对象的执行结果。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多