HttpModule,HttpHandler,HttpHandlerFactory

HttpModule:Http模块,能够在页面处理先后、应用程序初始化、出错等时候加入本身的事件处理程序.html

HttpHandler:Http处理程序,处理页面请求web

HttpHandlerFactory:用来建立Http处理程序,建立的同时能够附加本身的事件处理程序spring

1、HttpModule 这个对象咱们常常用来进行统一的权限判断、日志等处理app


 1public class MyModule : IHttpModule     
 2{         
 3    public void Init(HttpApplication application)         
 4    {             
 5        application.BeginRequest += new EventHandler(application_BeginRequest);         
 6    }
          
 7    void application_BeginRequest(object sender, EventArgs e)         
 8    {             
 9         ((HttpApplication)sender).Response.Write("Copyright @Gspring<br/>");         
10    }
          
11    public void Dispose(){}     
12}

web.config中配置:ide

< httpModules >          
< add name = " test "  type = " HttpHandle.MyModule, HttpHandle " />        
</ httpModules >

2、HttpHandler 这个对象常常用来加入特殊的后缀所对应的处理程序,好比能够限制.doc的文件只能给某个权限的人访问。 Asp.Net中的Page类就是一个IHttpHandler的实现例子代码: 网站

复制代码
 1 public   class  MyHandler : IHttpHandler     
 2 {         
 3    public void ProcessRequest(HttpContext ctx)         
 4    {             
 5      ctx.Response.Write("Copyright @Gspring<br/>");         
 6    }
         
 7    public bool IsReusable         
 8    {             
 9      get return true; }         
10    }
     
11}
 
复制代码

web.config中配置:ui

<httpHandlers>       
      
<add verb="*" path="*.aspx" type="HttpHandle.MyHandler, HttpHandle"/>       
</httpHandlers> url

这个对象主要就是ProcessRequest方法,在这个方法中输出版权信息,但同时也有一个问题:原来的页面不会被处理,也就是说页面中只有版权声明了。那么全部的aspx页面都不能正常运行了
3、HttpHandlerFactory 这个对象也能够用来加入特殊的后缀所对应的处理程序,它的功能比HttpHandler要更增强大,在系统的web.config中就是经过注册HttpHandlerFactory来实现aspx页面的访问的:
spa

       < add  path ="*.aspx"  verb ="*"  type ="System.Web.UI.PageHandlerFactory"  validate ="true" />

HttpHandlerFactory是HttpHandler的工厂,经过它来生成不一样的HttpHandler对象。 例子代码:日志

 

复制代码
public   class  MyHandlerFactory : IHttpHandlerFactory 
{
      
public  IHttpHandler GetHandler(HttpContext context,  string  requestType,  string  url,  string  pathTranslated)
      {
         PageHandlerFactory factory 
=  (PageHandlerFactory)Activator.CreateInstance( typeof (PageHandlerFactory),  true ); 
         IHttpHandler handler 
=  factory.GetHandler(context, requestType, url, pathTranslated); 
         Execute(handler); 
         
return  handler; 
      }
      
      
private   void  Execute(IHttpHandler handler) 
      {
          
if  (handler  is  Page) 
          {                 
// 能够直接对Page对象进行操做                 
               ((Page)handler).PreLoad  +=   new  EventHandler(MyHandlerFactory_PreLoad);  
                       
           }
      }

      
void  MyHandlerFactory_PreLoad( object  sender, EventArgs e)  
     {             
          ((Page)sender).Response.Write(
" Copyright @Gspring<br/> " );        
     }          
      
public   void  ReleaseHandler(IHttpHandler handler){ }     
}
复制代码

web.config中配置:

< httpHandlers >        
< add  verb ="*"  path ="*.aspx"  type ="HttpHandle.MyHandlerFactory, HttpHandle" />        
</ httpHandlers >

在例子中咱们经过调用系统默认的PageHandlerFactory类进行常规处理,而后在处理过程当中加入本身的代码,能够在Page对象上附加本身的事件处理程序。 附一个小的恶做剧: 咱们能够开发好aspx页面,而后把web应用程序发布后把全部的aspx文件的后缀都改成spring,再在web.config中加入配置:

< httpHandlers >        
< add  verb ="*"  path ="*.spring"  type ="HttpHandle.MyHandlerFactory, HttpHandle" />        
</ httpHandlers >        
< compilation >          
< buildProviders >            
< add  extension =".spring"  type ="System.Web.Compilation.PageBuildProvider" />          
</ buildProviders >        
</ compilation >

buildProviders是用来指定spring后缀的编译程序,咱们把它设置成和aspx一致就能够了。若是在IIS中发布的话还须要在应用程序配置中加入spring的后缀映射。而后咱们就能够经过 http://../.../*.spring来访问咱们的网站了

相关文章
相关标签/搜索