这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时候又不太确定。本文通过一个简单的例子来直观的比较一下这三个对象的使用。
HttpModule:Http模块,可以在页面处理前后、应用程序初始化、出错等时候加入自己的事件处理程序
HttpHandler:Http处理程序,处理页面请求
HttpHandlerFactory:用来创建Http处理程序,创建的同时可以附加自己的事件处理程序

例子很简单,就是在每个页面的头部加入一个版权声明。


一、HttpModule


这个对象我们经常用来进行统一的权限判断、日志等处理。
例子代码:

public class MyModule : IHttpModule
{
    public void Init(HttpApplication application)
    {
        application.BeginRequest += new EventHandler(application_BeginRequest);
    }

    void application_BeginRequest(object sender, EventArgs e)
    {
        ((HttpApplication)sender).Response.Write("Copyright @Gspring<br/>");
    }

    public void Dispose()
    {
    }
}

web.config中配置:

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

在Init方法中可以注册很多application的事件,我们的例子就是在开始请求的时候加入自己的代码,将版权声明加到页面的头部

二、HttpHandler


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

public class MyHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext ctx)
    {
        ctx.Response.Write("Copyright @Gspring<br/>");
    }
    public bool IsReusable
    {
        get { return true; }
    }
}

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

【IHttpHandler】HttpModule,HttpHandler,HttpHandlerFactory简单使用的更多相关文章

  1. HttpModule HttpHandler HttpHandlerFactory 学习笔记

    1.HttpModule 最常见的是使用HttpModule来做页面权限控制. 在新建类库添加如下代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  2. ASP.NET 管道事件与HttpModule, HttpHandler简单理解 -摘自网络

    第一部分:转载自Artech  IIS与ASP.NET管道 ASP.NET管道 以IIS 6.0为例,在工作进程w3wp.exe中,利用Aspnet_ispai.dll加载.NET运行时(如果.NET ...

  3. ASP.NET三剑客 HttpApplication HttpModule HttpHandler 解析

    我们都知道,ASP.Net运行时环境中处理请求是通过一系列对象来完成的,包含HttpApplication,HttpModule, HttpHandler.之所以将这三个对象称之为ASP.NET三剑客 ...

  4. HttpModule & HttpHandler

    ASP.NET 处理请求的过程 inetinfo.exe:www 服务进程,IIS 服务 和 ASPNET_ISAPI.dll 都寄存在此进程中. ASPNET_ISAPI.dll:处理 .aspx ...

  5. ASP.NET (HttpModule,HttpHandler)

    asp.net 事件模型机制 ----------------------- 一 客户的请求页面由aspnet_isapi.dll这个动态连接库来处理,把请求的aspx文件发送给CLR进行编译执行,然 ...

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

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

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

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

  8. ASP.NET 管道事件与HttpModule, HttpHandler简单理解

    BeginRequest 指示请求处理开始 AuthenticateRequest 封装请求身份验证过程 AuthorizeRequest 封装检查是否能利用以前缓存的输出页面处理请求的过程 Reso ...

  9. HttpModule和HttpHandler -- 系列文章

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

随机推荐

  1. ADF_ADF Faces系列4_ADF数据可视化组件简介之建立BarChart/Gauge/ExportExcel

    2013-05-01 Created By BaoXinjian

  2. PLSQL_基础系列07_插入方式Pivoting / Unconditional / Conditional ALL / Conditional FIRST INSERT(案例)

    2014-12-08 Created By BaoXinjian

  3. Edmonds_Karp 算法入门详解(转)

    转载自:http://blog.csdn.net/hsqlsd/article/details/7862903 有n个点,有m条有向边,有一个点很特殊,只出不进,叫做源点,通常规定为1号点.另一个点也 ...

  4. Redefining already defined constructor

    报错:Strict standards: Redefining already defined constructor for class SplitWord in D:\wamp\www\wsc\i ...

  5. Ubuntu打开终端和设置root密码(转载)

    From:http://blog.csdn.net/xhhjin/article/details/6328752 http://www.linuxsir.org/bbs/thread318516.ht ...

  6. Rspec中describe和context不同

    转自  http://lmws.net/describe-vs-context-in-rspec 学习rspec,不太理解describe和context.google了一下,找到这篇文章,感觉说的有 ...

  7. Eclipse 中使用 ctrl 无法追踪函数的问题

    Eclipse 中使用 ctrl 无法追踪函数的问题 Eclipse 项目中应该有 .buildpath , .project 两个文件,如果 Eclipse 中使用 ctrl 无法追踪函数, 第一步 ...

  8. Ext 项目随笔

    region: This region's layout position (north, south, east, west or center). Read-only. collapsible:t ...

  9. Java学习笔记——static关键字与静态的使用方法

    static:可以修饰成员变量和成员方法. 当变量被static修饰后,则其可以直接被类名调用.类名.成员. static特点: 随着类的加载而加载: 优先于对象存在: 被所有的对象共享,节省空间,但 ...

  10. 深度解析EM菌

    现在,在各大水族论坛里知道什么是EM菌的人不多,能正确说出EM菌的原理.成分和用途的人更是凤毛麟角,很多人对EM菌是否适用于水族箱存有疑虑,他们认为EM菌里大部分都是厌氧菌,因此不适合在水族箱这种好氧 ...