这三个对象我们在开发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. linux下安装mysql数据库与相关操作

    如下命令都是用root身份安装,或者在命令前加上sudo 采用yum安装方式安装 yum install mysql #安装mysql客户端 yum install mysql-server #安装m ...

  2. PLSQL_基础系列05_视图控制WITH CHECK OPTION(案例)

    2014-12-09 Created By BaoXinjian

  3. Codeforces Round #356 (Div. 2)B. Bear and Finding Criminals(水题)

    B. Bear and Finding Criminals time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  4. DB2 SQL性能调优秘笈

    SQL优化技巧 1.去除在谓词列上编写的任何标量函数 2.去除在谓词列上编写的任何数学运算 3.SQL语句的Select部分只写必要的列 4.尽可能不用Distinct 5.尽量将In子查询重写为Ex ...

  5. Apache2 CGI demo

    1. 修改 httpd.conf  配置 <IfModule alias_module> ScriptAlias /cgi-bin/ "/usr/local/apache2/cg ...

  6. Hbase原理

    Hbase原理 概述 HBase是一个构建在HDFS上的分布式列存储系统:HBase是基于Google BigTable模型开发的,典型的key/value系统:HBase是Apache Hadoop ...

  7. JAVA 下拉列表和滚动条

    //下拉列表和滚动条 import java.awt.*; import javax.swing.*; public class Jiemian7 extends JFrame{ JPanel mb1 ...

  8. android之SeekBar控件用法

    MainActivity.java package com.example.mars_2400_seekbar; import android.support.v7.app.ActionBarActi ...

  9. Failed to create the java virtual machine完全解决办法

    一直用EcliPSe开发java,突然有这么一天,无法启动了,splash窗口显示“Failed to create the Java Virtual Machine”,结果发现eclipse和mye ...

  10. 修改Tomcat的网站根目录

    想把Tomcat的默认网站根目录修改成自己指定的目录,比如:F:/MyWeb.这样以后把自己写的index.jsp放到该目录下,就能通过http://localhost:8080/index.jsp来 ...