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的实现例子代码:


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

HttpModule,HttpHandler,HttpHandlerFactory的更多相关文章

  1. 【IHttpHandler】HttpModule,HttpHandler,HttpHandlerFactory简单使用

    这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时候又不太确定.本文通过一个简单的例子来直观的比较一下这三个对象的使用. HttpModule:Http模块,可以在页面处理前后.应 ...

  2. C#强化系列:HttpModule,HttpHandler,HttpHandlerFactory简单使用

    这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时候又不太确定.本文通过一个简单的例子来直观的比较一下这三个对象的使用.HttpModule:Http模块,可以在页面处理前后.应用 ...

  3. HttpModule和HttpHandler -- 系列文章

    ASP.NET 生命周期 在ASP.Net2.0中使用UrlRewritingNet实现链接重写 IHttpModule实现URL重写 使用IHttpHandler防盗链 HttpModule,Htt ...

  4. HttpModule、HttpHandler和Page的生命周期

    1.引言 2.两个处理步骤 2.1.创建ASP.NET环境 2.2.用触发的MHPM事件处理请求 3.什么事件中应该做什么 4.示例代码 5.深入ASP.NET页面事件 1.引言 这篇文章我们将试图理 ...

  5. HttpHandler,HttpApplication, HttpModule

    选择HttpHandler还是HttpModule? HttpHandler和HttpModule之间有什么差别 之所以有这个疑问,是因为在这二类对象中都可以访问Request, Response对象 ...

  6. Asp.net下使用HttpModule模拟Filter,实现权限控制

    在asp.net中,我们为了防止用户直接从Url中访问指定的页面而绕过登录验证,需要给每个页面加上验证,或者是在模板页中加上验证.如果说项目比较大的话,添加验证是一件令人抓狂的事情,本次,我就跟大家分 ...

  7. ASP.NET-自定义HttpModule与HttpHandler

    在之前的ASP.NET是如何在IIS下工作的这篇文章中介绍了ASP.NET与IIS配合工作的机制,在http请求经过一系列处理后,最后到达ASP.NET管道中,这时,就是Http Modules和Ht ...

  8. HttpModule与HttpHandler详解

    ASP.NET对请求处理的过程:当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给 ASPNET_ISAPI.dll,A ...

  9. Asp.net中的HttpModule和HttpHandler的简单用法

    在Asp.net中,HttpModule和HttpHandler均可以截取IIS消息进行处理,这使得我们制作人员能够非常方便的进行诸如图片水印添加,图片盗链检查等功能. 下面先就HttpModule的 ...

随机推荐

  1. 【BZOJ 2333 】[SCOI2011]棘手的操作(离线+线段树)

    2333: [SCOI2011]棘手的操作 Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边 ...

  2. The APR based Apache Tomcat Native library

    Tomcat启动的时候出现下面这样的提示: 2015-11-06 14:24:12 org.apache.catalina.core.AprLifecycleListener init 信息: The ...

  3. 使用solrj进行DIH操作

    背景说明:在一个项目中需要将Mongodb中的数据导入到solr中完成搜索.在solr中Mysql数据库有对应的DIH包,可以通过配置sql语句完成数据的导入.Mongodb下也有开源的工具用来实现数 ...

  4. ANDROID_MARS学习笔记_S04_003_用HttpClent发http请求

    一.代码 1.xml(1)activity_main.xml <TextView android:layout_width="wrap_content" android:la ...

  5. 实现ImageView中两张图片重叠显示

    第一种XML配置 使用layer-list标签 <layer-list xmlns:android="http://schemas.android.com/apk/res/androi ...

  6. 转载:java保留2位小数

    转载:http://blog.csdn.net/wj_j2ee/article/details/8560132 java保留两位小数问题: 方式一: 四舍五入  double   f   =   11 ...

  7. A WCF-WPF Chat Application

    http://www.codeproject.com/Articles/25261/A-WCF-WPF-Chat-Application

  8. C# partial 局部类型

    关键字partial是一个上下文关键字,只有和 class.struct.interface 放在一起时才有关键字的含义.因此partial的引入不会影响现有代码中名称为partial的变量.局部类型 ...

  9. HDOJ/HDU 1085 Holding Bin-Laden Captive!(非母函数求解)

    Problem Description We all know that Bin-Laden is a notorious terrorist, and he has disappeared for ...

  10. 1002: Prime Path

    题目链接:http://172.16.200.33/JudgeOnline/problem.php?id=1002 题意:给你两个四位数的素数,求最少经过多少步的变化能够从一个素数变到另一个素数.在变 ...