原文地址:http://www.cnblogs.com/skm-blog/p/3178862.html

上文讲到了HttpRunTime主要做了三个事情,我们先回忆一下。

第一:雇佣了项目经理(HttpApplication)。

第二:建立了HttpModule列表,项目经理(HttpRunTime)就是按照这个工作列表去工作的。

第三:创建了Context上下文对象(包含了HttpRequest和HttpResponse两大主要对象),并把它转交给了HttpApplication去处理。

下面最重要的是HttpModule到底是个什么东东,HttpApplication又是怎么样按照它的工作列表去工作的。

当一个请求到达HttpModule时,整个Asp.Net系统并没有对这个Http请求做任何的处理,对于一个Http请求,HttpModule是这个请求的必经之路{Asp.Net内部的处理模型是一个管道流,也就是用户发出一个Http请求一定会从HttpModule这个容器中流过},所以我们可以在Http请求真正到达请求处理中心(HttpHandler)之前,添加一些需要的信息在这个Http请求上、或者针对截获的这个Http请求添加一些额外的工作,再或者干脆终止此次Http请求。可见HttpModule就相当于起到一个Filter过滤器的作用。

1、asp.net的HTTP请求处理过程

说明:
(1)、客户端浏览器向服务器发出一个http请求,此请求会被inetinfo.exe进程 截获,然后转交给aspnet_isapi.dll进程,接着它又通过Http Pipeline的管道,传送给aspnet_wp.exe这个进程,接下来就到了.net framework的HttpRunTime处理中心,处理完毕后就发送给用户浏览器。
(2)、当一个 http请求被送入到HttpRuntime之后,这个Http请求会继续被送入到一个被称之为HttpApplication Factory的一个容器当中,而这个容器会给出一个HttpApplication实例来处理传递进来的http请求,而后这个Http请求会依次进入 到如下几个容器中:HttpModule --> HttpHandler Factory --> HttpHandler。当系统内部的HttpHandler的ProcessRequest方法处理完毕之后,整个Http Request就被处理完成了,客户端也就得到相应的东东了。
(3)完整的http请求在asp.net framework中的处理流程:
HttpRequest-->inetinfo.exe->ASPNET_ISAPI.DLL-->Http Pipeline-->ASPNET_WP.EXE-->HttpRuntime-->HttpApplication Factory-->HttpApplication-->HttpModule-->HttpHandler Factory-->HttpHandler-->HttpHandler.ProcessRequest()

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

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

2、HttpModule的工作原理

HttpModule实现了IHttpModule接口,我们可以定义自己类实现IHttpModule接口,从而把我们自己定义的HttpModule插入到HttpModule集合中让它成为Http请求的必经之路,以此来添加我们的控制。

asp.net自带的HttpModule如下:具体位置在C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG文件中

  <httpModules>
<add name="OutputCache" type="System.Web.Caching.OutputCacheModule"/>
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
<add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule"/>
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule"/>
<add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule"/>
<add name="RoleManager" type="System.Web.Security.RoleManagerModule"/>
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule"/>
<add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule"/>
<add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule"/>
<add name="Profile" type="System.Web.Profile.ProfileModule"/>
<add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</httpModules>

这里的每个HttpModule在一个Http请求过来时都会流过。

3、我们可以定义自己的HttpModule

要变成HttpModule就必须实现IHttpModule接口,那么我们先来看看IHttpModule接口的内部是什么样的

using System;

namespace System.Web

{

    public interface IHttpModule

    {

        //   销毁不再被HttpModule使用的资源

        void Dispose();

        // 初始化一个Module,为捕获HttpRequest做准备

        void Init(HttpApplication context);

    }

}
下面是自己定义的HttpModule
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace ClassLibrary1
{
public class MyHttpModel:IHttpModule
{ public void Dispose()
{
throw new NotImplementedException();
} public void Init(HttpApplication context)
{
context.BeginRequest+=new EventHandler(context_BeginRequest);
context.EndRequest+=new EventHandler(context_EndRequest);
}
public void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpContext context = application.Context;
HttpResponse response = context.Response;
response.Write("这是自定义的HttpModel中的BeginRequest事件产生的。");
}
public void context_EndRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpContext context = application.Context;
HttpResponse response = context.Response;
response.Write("这是自定义的HttpModel中的EndRequest事件产生的。");
}
}
}

置文件如下

<httpModules>
      <add name="MyHttpModel" type="ClassLibrary1.MyHttpModel,ClassLibrary1"/>
</httpModules>

请求的Default.aspx.cs文件如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace ClassLibrary1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("这是Default.aspx页面请求的内容信息。");
}
}
}

具体运行结果如下:

今天就写到这里吧,文章页有些长了,不过HttpModule还没有介绍完呢,如果您对我的文章感兴趣的话请继续关注我的下篇,下篇我们将继续介绍HttpModule这个东东的。

本系列:

一:Asp.Net生命周期系列一

二:Asp.Net生命周期系列二

(转)Asp.Net生命周期系列三的更多相关文章

  1. Asp.Net生命周期系列三

    上文讲到了HttpRunTime主要做了三个事情,我们先回忆一下. 第一:雇佣了项目经理(HttpApplication). 第二:建立了HttpModule列表,项目经理(HttpRunTime)就 ...

  2. Asp.Net生命周期系列四

    上回我们说的当一个Http请求来到HttpModule这里的时候,Asp.Net内部并未对这个Http请求做出任何的处理,我们可以对这个Http请求添加一些我们需要的信息,以方便我们控制这个Http请 ...

  3. Asp.Net生命周期系列五

    如果您看了我的前四篇文章,应该知道目前Http请求已经流到了HttpModule这个程序员手中了,而且我们可以注册自己的HttpModule并且可以在里面注册一些事件来控制这个Http请求,但是到目前 ...

  4. (转)Asp.Net生命周期系列五

    原文地址:http://www.cnblogs.com/skm-blog/p/3188697.html 如果您看了我的前四篇文章,应该知道目前Http请求已经流到了HttpModule这个程序员手中了 ...

  5. Asp.Net生命周期系列二

    在上回书开始的时候我们提到博客园的IIS看了一眼我的请求后就直接交给ASP.NET去处理了,并且要求ASP.NET处理完之后返回HTML以供展示. 那么我们不仅要问: 1,    IIS肯定是没有眼睛 ...

  6. Asp.Net生命周期系列六

    上篇说到当一个Http请求流到HttpHandler这里时才开始对它的处理,那么一个请求经过HttpHandler之后, 到底怎么对它处理呢,也就是说HttpHandler会触发哪些事件,触发的顺序如 ...

  7. (转)Asp.Net生命周期系列一

    原文地址:http://www.cnblogs.com/skm-blog/archive/2013/07/07/3176713.html Asp.Net生命周期对于初级甚至中级程序员来说,一直都是一个 ...

  8. Asp.Net生命周期系列一

    Asp.Net生命周期对于初级甚至中级程序员来说,一直都是一个难题,很多程序员不了解生命周期,导致使用Asp.Net做开发感觉很不灵活,感觉太多东西被微软封装好了,我们不能改变,其实只要你稍微了解一下 ...

  9. ASP.NET生命周期详解

    最近一直在学习ASP.NET MVC的生命周期,发现ASP.NET MVC是建立在ASP.NET Framework基础之上的,所以原来对于ASP.NET WebForm中的很多处理流程,如管道事件等 ...

随机推荐

  1. 百度浏览器极速模式下访问 FastAdmin 的问题

    百度浏览器极速模式下访问 FastAdmin 的问题 兼容性问题,因为 https 证书配置时对低版本的浏览器不适配引起. 应该是 百度浏览器的内核太旧,没有更新导致.

  2. Linux 定时任务 crontab 讲解

    linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另 外, 由于使用者自己也可以设置计划任务,所以, ...

  3. DHCP(六)

    DHCP报文: DHCP共有八种报文,分别为DHCP Discover.DHCP Offer.DHCP Request.DHCP ACK.DHCP NAK.DHCP Release.DHCP Decl ...

  4. android httpclient 设置超时

    3.X是这样的 HttpClient httpClient=new DefaultHttpClient();4.3是这样的CloseableHttpClient httpClient = HttpCl ...

  5. selenium 定位元素可以用多个findElement

    selenium 定位元素可以用多个findElement,如driver.findElement(By.id"XXX").findElement(By.linkText(XXX) ...

  6. php图像处理函数imagecopyresampled

    语法 bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , i ...

  7. Python基础学习七 网络编程

    主要应用urllib和requests模块 urllib模块返回类型为bytes,需要数据类型转换:requests就方便很多. 例子1:发送post请求 url = 'http://api.nnzh ...

  8. Python基础学习六 操作Redis

    import redis r = redis.Redis(host=',db=1) #set get delete setex 都是针对string类型的 k-v # r.set('louis_ses ...

  9. Java字节码

    Java字节码 javap -c 反编译.class文件可得字节码 知乎讨论https://www.zhihu.com/question/27831730 栈和局部变量操作 将常量压入栈的指令 aco ...

  10. RT2870移植到s3c2416后续验证无线…

    我的无线网卡显示的事ra0,所以把下面的wlan0换成ra0即可:视自己的情况而定 1. 打开无线网卡电源 iwconfig wlan0 txpower on 2. 列出区域内的无线网络 iwlist ...