HttpModule HttpHandler HttpHandlerFactory 学习笔记

1。HttpModule

    最常见的是使用HttpModule来做页面权限控制。

    在新建类库添加如下代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace HttpModuleCK
{

    public class UserControl : IHttpModule
    {
        public void Dispose()
        { }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new System.EventHandler(httpApplication_BeginRequest);
        }

        public void httpApplication_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication httpApplication = (HttpApplication)sender;
            string url = httpApplication.Context.Request.Path;
            //做用户权限控制
            HttpContext content = (HttpContext)httpApplication.Context;

            //if (httpApplication.Request["userid"] == null)
            //{
            //    content.Response.Write("未提供必需的参数!!");
            //    content.Response.End();
            //}
        }




    }
}

   在Web项目中添加引用该类,添加配置信息即可,

<httpModules>
      <add name="UserRight" type="HttpModuleCK.UserControl, HttpModuleCK"/>	
		</httpModules>

2.HttpHandler

这个常用来做文件访问控制,图片防盗链等等.

新建类库添加如下代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace HttpHandlerCK
{
    public class docHandle:IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string FileName = context.Server.MapPath(context.Request.FilePath);
            if (context.Request["userid"] == null)
            {
                context.Response.Write("doc文件无法访问!!");
                context.Response.End();
            }
           
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
在Web项目的config中配置如下:
	<httpHandlers>
      <add verb="*" path="*.doc" type="HttpHandlerCK.docHandle, HttpHandlerCK"/>
         </httpHandlers>

3.HttpHandlerFactory

  功能比HttpHandler要更加强大,HttpHandlerFactory是HttpHandler的工厂,通过它来生成不同的HttpHandler对象。

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace HandlerFactoryCK
{
    public class aspxHandle: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)
        {

        }

    }
}
  在Webconfig配置如下:
	<httpHandlers>
      <add verb="*" path="*.aspx" type="HandlerFactoryCK.aspxHandle, HandlerFactoryCK"/>
</httpHandlers>

三者的区别如下:

  生命周期中涉及到几个非常重要的对象:HttpHandler,HttpModule,IHttpHandlerFactory,他们的执行(顺序)大致的执行过程是这样的:

client端发送页面请求,被IIS的某个进程截获,它根据申请的页面后缀(.aspx)不同,调用不同的页面处理程序(.asp->asp.dll; .aspx->ISAPI.dll)。而页面处理程序在处理过程中,则要经历HttpModule,HttpHandler的处理:前者HttpModule用于页面处理前和处理后的一些事件的处理,后者HttpHandler进行真正的页面的处理。
如前所说,HttpModule会在页面处理前和后对页面进行处理,所以它不会影响真正的页面请求。通常用在给每个页面的头部或者尾部添加一些信息(如版权声明)等。曾经见过一些免费的空间,我们的页面上传上去后,浏览的时候发现,在每个页面的头部和尾部多了很多小广告....如果理解了HttpModule的原理,要做这个就不是很难了~

IHttpModule与IHttpHandler的区别整理
1.先后次序.先IHttpModule,后IHttpHandler. 注:Module要看你响应了哪个事件,一些事件是在Handler之前运行的,一些是在Handler之后运行的
2.对请求的处理上:
   IHttpModule是属于大小通吃类型,无论客户端请求的是什么文件,都会调用到它;例如aspx,rar,html的请求.
   IHttpHandler则属于挑食类型,只有ASP.net注册过的文件类型(例如aspx,asmx等等)才会轮到调用它.
3.IHttpHandler按照你的请求生成响应的内容,IHttpModule对请求进行预处理,如验证、修改、过滤等等,同时也可以对响应进行处理

相关资料:http://www.cnblogs.com/cyan/archive/2009/02/04/1383580.html

              http://www.cnblogs.com/firstyi/archive/2008/05/07/1187274.html

             http://www.cnblogs.com/zc22/archive/2007/12/04/982963.html

原文地址:https://www.cnblogs.com/dooom/p/1845981.html