注:本文为个人学习摘录,原文地址:http://www.cnblogs.com/stwyhm/archive/2006/08/09/471765.html

HttpModule是如何工作的

当一个HTTP请求到达HttpModule时,整个ASP.NET Framework系统还并没有对这个HTTP请求做任何处理,也就是说此时对于HTTP请求来讲,HttpModule是一个HTTP请求的“必经之路”,所以可以在这个HTTP请求传递到真正的请求处理中心(HttpHandler)之前附加一些需要的信息在这个HTTP请求信息之上,或者针对截获的这个HTTP请求信息作一些额外的工作,或者在某些情况下干脆终止满足一些条件的HTTP请求,从而可以起到一个Filter过滤器的作用。

示例1:

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

namespace MyHttpModule

{

/// <summary>

/// 说明:用来实现自己的HttpModule类。

/// 作者:文野

/// 联系:stwyhm@cnblogs.com

/// </summary>

public class MyFirstHttpModule : IHttpModule

{

private void Application_BeginRequest(object sender, EventArgs e)

{

HttpApplication application = (HttpApplication)sender;

HttpContext context = application.Context;

HttpRequest request = application.Request;

HttpResponse response = application.Response;

response.Write("我来自自定义HttpModule中的BeginRequest<br />");

}

private void Application_EndRequest(object sender, EventArgs e)

{

HttpApplication application = (HttpApplication)sender;

HttpContext context = application.Context;

HttpRequest request = application.Request;

HttpResponse response = application.Response;

response.Write("我来自自定义HttpModule中的EndRequest<br />");

}

#region IHttpModule 成员

public void Dispose()

{}

public void Init(HttpApplication application)

{

application.BeginRequest += new EventHandler(Application_BeginRequest);

application.EndRequest += new EventHandler(Application_EndRequest);

}

#endregion

}

}

在Web.config进行如下配置

在 <system.web>  </system.web>中添加如下配置

<httpModules>
  <add name="MyFirstHttpModule" type="MvcApplicationTest.MyFirstHttpModule,MvcApplicationTest" />
</httpModules>

如果是部署在IIS7下,则需要在<system.webServer></system.webServer>中添加如下配置

<modules>
  <add name="MyFirstHttpModule" type="MvcApplicationTest.MyFirstHttpModule,MvcApplicationTest" preCondition="integratedMode" />
</modules>

访问结果如下

深入了解HttpModule

一个HTTP请求在HttpModule容器的传递过程中,会在某一时刻(ResolveRequestCache事件)将这个HTTP请求传递给HttpHandler容器。在这个事件之后,HttpModule容器会建立一个HttpHandler的入口实例,但是此时并没有将HTTP请求控制权交出,而是继续触发AcquireRequestState事件以及PreRequestHandlerExcute事件。在PreRequestHandlerExcute事件之后,HttpModule窗口就会将控制权暂时交给HttpHandler容器,以便进行真正的HTTP请求处理工作。

而在HttpHandler容器内部会执行ProcessRequest方法来处理HTTP请求。在容器HttpHandler处理完毕整个HTTP请求之后,会将控制权交还给HttpModule,HttpModule则会继续对处理完毕的HTTP请求信息流进行层层的转交动作,直到返回到客户端为止。

图1:HttpModule生命周期示意图

示例2:验证HttpModule生命周期

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

namespace MyHttpModule

{

public class ValidaterHttpModule : IHttpModule

{

#region IHttpModule 成员

public void Dispose()

{}

public void Init(HttpApplication application)

{

application.BeginRequest += new EventHandler(application_BeginRequest);

application.EndRequest += new EventHandler(application_EndRequest);

application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);

application.PostRequestHandlerExecute += new EventHandler(application_PostRequestHandlerExecute);

application.ReleaseRequestState += new EventHandler(application_ReleaseRequestState);

application.AcquireRequestState += new EventHandler(application_AcquireRequestState);

application.AuthenticateRequest += new EventHandler(application_AuthenticateRequest);

application.AuthorizeRequest += new EventHandler(application_AuthorizeRequest);

application.ResolveRequestCache += new EventHandler(application_ResolveRequestCache);

application.PreSendRequestHeaders += new EventHandler(application_PreSendRequestHeaders);

application.PreSendRequestContent += new EventHandler(application_PreSendRequestContent);

}

void application_PreSendRequestContent(object sender, EventArgs e)

{

HttpApplication application = (HttpApplication)sender;

application.Context.Response.Write("application_PreSendRequestContent<br/>");

}

void application_PreSendRequestHeaders(object sender, EventArgs e)

{

HttpApplication application = (HttpApplication)sender;

application.Context.Response.Write("application_PreSendRequestHeaders<br/>");

}

void application_ResolveRequestCache(object sender, EventArgs e)

{

HttpApplication application = (HttpApplication)sender;

application.Context.Response.Write("application_ResolveRequestCache<br/>");

}

void application_AuthorizeRequest(object sender, EventArgs e)

{

HttpApplication application = (HttpApplication)sender;

application.Context.Response.Write("application_AuthorizeRequest<br/>");

}

void application_AuthenticateRequest(object sender, EventArgs e)

{

HttpApplication application = (HttpApplication)sender;

application.Context.Response.Write("application_AuthenticateRequest<br/>");

}

void application_AcquireRequestState(object sender, EventArgs e)

{

HttpApplication application = (HttpApplication)sender;

application.Context.Response.Write("application_AcquireRequestState<br/>");

}

void application_ReleaseRequestState(object sender, EventArgs e)

{

HttpApplication application = (HttpApplication)sender;

application.Context.Response.Write("application_ReleaseRequestState<br/>");

}

void application_PostRequestHandlerExecute(object sender, EventArgs e)

{

HttpApplication application = (HttpApplication)sender;

application.Context.Response.Write("application_PostRequestHandlerExecute<br/>");

}

void application_PreRequestHandlerExecute(object sender, EventArgs e)

{

HttpApplication application = (HttpApplication)sender;

application.Context.Response.Write("application_PreRequestHandlerExecute<br/>");

}

void application_EndRequest(object sender, EventArgs e)

{

HttpApplication application = (HttpApplication)sender;

application.Context.Response.Write("application_EndRequest<br/>");

}

void application_BeginRequest(object sender, EventArgs e)

{

HttpApplication application = (HttpApplication)sender;

application.Context.Response.Write("application_BeginRequest<br/>");

}

#endregion

}

}

多个自定义的Http Module的运作

从运行结果可以看到,在web.config文件中引入自定义HttpModule的顺序就决定了多个自定义HttpModule在处理一个HTTP请求的接管顺序。注:系统默认那几个HttpModule是最先衩ASP.NET Framework所加载上去的。

示例3:(代码类同示例2)

HttpModule中终止此次的HTTP请求

可以利用HttpModule通过调用HttpApplication.CompleteRequest()方法实现当满足某一个条件时终止此次的HTTP请求。

需要注意的是,即使调用了HttpApplication.CompleteRequest()方法终止了一个HTTP请求,ASP.NET Framework仍然会触发HttpApplication后面的这3个事件:EndRequest事件、PreSendRequestHeaders事件、PreSendRequestContent事件。

如果存在多个自定义的HttpModule的话,当Module1终止了一个HTTP请求,这个HTTP请求将不会再触发Module2中相应的事件了,但Module2的最后三个事件仍会被触发。

示例4:

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

namespace MyHttpModule

{

public class CompleteRequestHttpModule : IHttpModule

{

#region IHttpModule 成员

public void Dispose()

{}

public void Init(HttpApplication application)

{

application.BeginRequest += new EventHandler(Application_BeginRequest);

}

void Application_BeginRequest(object sender, EventArgs e)

{

HttpApplication application = (HttpApplication)sender;

application.CompleteRequest();

application.Context.Response.Write("请求被终止。");

}

#endregion

}

}

HttpModule的基本概念的更多相关文章

  1. MVC5-1 ASP.NET的管道流

    MVC5 和WebForm的区别 WebForm是一个Page贯穿了一个.CS代码. 1对1 = 耦合在一起 MVC在Controller中将 bihind和page进行了分离. 多对多 = 松耦合 ...

  2. 【转】一点一点学ASP.NET之基础概念——HttpModule

    概述 HttpHandler是一个HTTP请求的真正处理中心,也正是在这个HttpHandler容器中,ASP.NET Framework才真正地对客户端请求的服务器页面做出编译和执行,并将处理过后的 ...

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

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

  4. 【N年前的文章脑补:HttpHandler HttpModule入门篇】

    HttpHandler HttpModule入门篇 ASP.Net处理Http Request时,使用Pipeline(管道)方式,由各个HttpModule对请求进行处理,然后到达 HttpHand ...

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

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

  6. httpappplication 和 httpmodule 的理解(转载,写的很好)

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

  7. IIS Web 服务器/ASP.NET 运行原理基本知识概念整理 转

    转http://www.cnblogs.com/loongsoft/p/7272830.html IIS Web 服务器/ASP.NET 运行原理基本知识概念整理  前言:      记录 IIS 相 ...

  8. IIS Web 服务器/ASP.NET 运行原理基本知识概念整理

     前言:      记录 IIS 相关的笔记还是从公司笔试考核题开始的,问 Application Pool 与 AppDomain 的区别?      促使我对进程池进了知识的学习,所以记录一下学习 ...

  9. 如何一步一步用DDD设计一个电商网站(一)—— 先理解核心概念

    一.前言     DDD(领域驱动设计)的一些介绍网上资料很多,这里就不继续描述了.自己使用领域驱动设计摸滚打爬也有2年多的时间,出于对知识的总结和分享,也是对自我理解的一个公开检验,介于博客园这个平 ...

随机推荐

  1. Python基础之列表

    列表的内置方法 1.append() 描述:用于在列表末尾添加新的对象. 示例: msg_list=["aaaa","bbbbb","cccccc&q ...

  2. UGUI事件系统

    UGUI系统 将UI可能触发的事件分为12个类型,即EventTriggerType枚举的12个值. PointerEnter-- PointerExit-- PointerDown-- Pointe ...

  3. 修改searchBar的返回按钮的显示文字

    #pragma mark 搜索框的代理方法,搜索输入框获得焦点(聚焦) - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { ...

  4. 面向对象UML中类关系

    如果你确定两件对象之间是is-a的关系,那么此时你应该使用继承:比如菱形.圆形和方形都是形状的一种,那么他们都应该从形状类继承而不是聚合.如果你确定两件对象之间是has-a的关系,那么此时你应该使用聚 ...

  5. 关于Python2字符编码的体会

    对于Python的字符编码问题也懵了很久,最近做爬虫多次遇到网页转码的问题,干脆彻底解决掉!Just Do it! 1.两种类型str与unicode str和unicode都是basestring的 ...

  6. Dojo的UI框架bootstrap for dojo和Dojo-Bootstrap简介

    最近在学习Dojo,但样式问题真是头疼呀,虽然清新的淡蓝色很是减缓眼睛的疲劳,但这个扁平化简约风盛行的年代,光是清新的拟物已经满足不了群众的需求了,所以就在这样的需求下,我知道了bootstrap f ...

  7. c#代码发送post请求,上传文件(并带其他参数)

    本人对post理解不深,前段时间遇到一个需要用c#代码发送post请求上传文件的业务,于是参考了几篇帖子,加上自身实践写出了如下代码.写的比较low 望各位大大指正^_^. 业务需求: 对方给了一个接 ...

  8. ng-Directive

    伪代码: var myModule = angular.module(...); myModule.directive('namespaceDirectiveName', function facto ...

  9. jquery的animate动画

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. Link方式导入java项目

    一.环境说明 我用的是eclipse3.3.2,并安装了myeclipse的插件. 二.工程文件说明 一共三个文件夹: javacode:存放Java源文件,包含多个子文件夹,每个子文件夹表示一个单独 ...