HttpHandler

如果说HttpModule相当于IIS的ISAPI Filter的话,我们可以说HttpHandler则相当于IIS的ISAPI Extension,HttpHandler在ASP.NET中扮演请求的最终处理者的角色。对于不同资源类型的请求,ASP.NET会加载不同的Handler来处理,也就是说.aspx page与.asmx web service对应的Handler是不同的。

所有的HttpHandler都实现了接口IHttpHandler。下面是IHttpHandler的定义,方法ProcessRequest提供了处理请求的实现。

   1: public interface IHttpHandler
   2: {
   3:     void ProcessRequest(HttpContext context);
   4:     bool IsReusable { get; }
   5: }

对于某些HttpHandler,具有一个与之相关的HttpHandlerFactory,用于创建或者获取相应的HttpHandler。HttpHandlerFactory实现接口IHttpHandlerFactory,方法GetHandler用于创建新的HttpHandler,或者获取已经存在的HttpHandler。

   1: public interface IHttpHandlerFactory
   2: {
   3:     IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated);
   4:     void ReleaseHandler(IHttpHandler handler);
   5: }

HttpHandler和HttpHandlerFactory的类型都可以通过相同的方式配置到Web.config中。下面一段配置包含对3种典型的资源类型的HttpHandler配置:.aspx,.asmx和.svc。可以看到基于WCF Service的HttpHandler类型为:System.ServiceModel.Activation.HttpHandler。

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3: <system.web>
   4: <httpHandlers>
   5: <add path="*.svc" verb="*" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="false"/>
   6: <add path="*.aspx" verb="*" type="System.Web.UI.PageHandlerFactory" validate="True"/>
   7: <add path="*.asmx" verb="*" type="System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="False"/>
   8: </httpHandlers>
   9: </system.web>
  10: </configuration>
 
posted @ 2013-02-22 13:38  史红星-shihongxing  阅读(230)  评论(0编辑  收藏  举报