HttpModule,HttpHandler,HttpHandlerFactory

HttpModule:Http模块,可以在页面处理前后、应用程序初始化、出错等时候加入自己的事件处理程序.

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

HttpHandlerFactory:用来创建Http处理程序,创建的同时可以附加自己的事件处理程序

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


 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中配置:

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

二、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中配置:

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

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

       < 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来访问我们的网站了

转载于:https://www.cnblogs.com/sunshch/p/3691434.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值