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來訪問咱們的網站了

相關文章
相關標籤/搜索