HttpHandler对象必须实现IHttpHandler接口,在HttpApplication对象转交要求至HttpHandler对象时会调用此接口中的init函数,下面是一个HelloHandler类的程序。
- using System;
- using System.Web;
- using System.Web.SessionState;
-
- namespace c64.HttpHandler
- {
- public class HelloHandler : IHttpHandler,IRequiresSessionState
- {
- #region IHttpHandler Members
-
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/html";
- context.Response.Write("<html>");
- context.Response.Write("<body>");
- context.Response.Write("<B>Response By HelloHandler</B>");
- context.Response.Write("</body");
- context.Response.Write("</html>");
- }
-
- public bool IsReusable
- {
- get
- {
- return true;
- }
- }
-
- #endregion
- }
- }
挂载这个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类源代码。
- using System;
- using System.Web;
- using System.Web.SessionState;
-
- namespace c64.HttpHandler
- {
- public class HelloHandler : IHttpHandler,IRequiresSessionState
- {
- #region IHttpHandler Members
-
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/html";
- context.Response.Write("<html>");
- context.Response.Write("<body>");
- context.Response.Write("<B>Response By HelloHandler</B>");
- string user = (string)context.Session["USER"];
- if(user != null && user.Length > 0)
- context.Response.Write(user);
- context.Response.Write("</body");
- context.Response.Write("</html>");
- }
-
- public bool IsReusable
- {
- get
- {
- return true;
- }
- }
-
- #endregion
- }
- }
要测试这个程序,我们可以在WebForm上放置一个按钮,当用户按下后存放一个字符串到Session["USER"]中,接着要求.ho的文件就可以看到结果。
实现HttpModule
与HttpHandler对象的加载时机不同,HttpModule对象是在HttpApplication对象初始化时就一并载入的,这个时机是在本机开机后第一个用户访问此虚拟目录时。设计人员撰写HttpModule对象的机会不多,甚至比撰写HttpHandler对象的机会还少。下面撰写一个HelloModule对象来示范如何撰写HttpModule对象。
- using System;
- using System.Web;
- using System.Web.SessionState;
-
- namespace HttpModule
- {
- public class HelloModule : IHttpModule,IRequiresSessionState
- {
- #region IHttpModule Members
-
- public void Init(HttpApplication context)
- {
- context.Application["HELLO"] = "I am Hello Module!";
- context.Application[GetHashCode().ToString()] = 123;
- }
-
- public void Dispose()
- {
- }
-
- #endregion
- }
- }
这个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对象的执行结果。