Asp.Net生命周期系列四

上回我们说的当一个Http请求来到HttpModule这里的时候,Asp.Net内部并未对这个Http请求做出任何的处理,我们可以对这个Http请求添加一些我们需要的信息,以方便我们控制这个Http请求。我们添加控制信息一般情况下是通过添加一些事件来控制的,那么HttpModule内部到底有哪些事件呢,他们的执行顺序又是怎样的呢?

1、HttpModule的事件

   
BeginRequest 指示请求处理开始
AuthenticateRequest 封装请求身份验证过程
AuthorizeRequest 封装检查是否能利用以前缓存的输出页面处理请求的过程
ResolveRequestCache 从缓存中得到相应时候触发
AcquireRequestState 加载初始化Session时候触发
PreRequestHandlerExecute 在Http请求进入HttpHandler之前触发
PostRequestHandlerExecute 在Http请求进入HttpHandler之后触发
ReleaseRequestState 存储Session状态时候触发
UpdateRequestCache 更新缓存信息时触发
EndRequest 在Http请求处理完成的时候触发
PreSendRequestHenaders 在向客户端发送Header之前触发
PreSendRequestConternt 在向客户端发送内容之前触发

2、验证HttpModule生命周期
与HttpHandler的交互:
Asp.Net生命周期系列四_第1张图片

说明:
a、HttpModule容器会将HttpRequest传递到HttpHandler容器,这个时间点是ResolveRequestCache事件
b、HttpModule容器会建立HttpHandler实例作为入口——Session从此生效
c、触发AcquireRequestState事件以及PreRequestHandlerExecute事件
d、HttpModule容器便将对HttpRequest的控制权转让给HttpHandler容器
e、HttpHandler容器处理HttpRequest——使用自身的ProcessRequest方法,处理完后将对其控制权又还给HttpModule容器——之后Session失效。

在系列一种我们说到,当我们的项目经理(HttpApplication)拿到包含有我们整个请求信息的上下文对象Context(内部包含主要的Request对象和Response对象)之后。它会按照一个由HttpRunTime事先根据webconfig文件建立好的HttpModule列表去工作,其实啊,就是按照这样一个表的顺序去把Http请求流【传递】一遍罢了。

那么现在我有个问题想问一下大家,你们认为这个Http请求究竟会怎么样流过这些HttpModule呢?

我的配置文件是这样写的:(MyHttpModule1已经注释掉了哦,看清楚,写系列三时用到了MyHttpModule1,)

    <httpModules>
      <!--<add name="MyHttpModel"  type="ClassLibrary1.MyHttpModel,ClassLibrary1"/>-->
      <add name="MyHttpModel2" type="ClassLibrary1.MyHttpModule2,ClassLibrary1"/>
      <add name="MyHttpModel3" type="ClassLibrary1.MyHttpModule3,ClassLibrary1"/>
    </httpModules>

我给你们两种选择:

第一种:谁在前面谁是老大啊,那自然是先流过MyHttpModule2,过完之后才能轮到MyHttpModule3啊,这就是所谓的先来先服务吗,谁让人家比你靠前呢。

第二种:凭什么你MyHttpModule2在前面Http请求就要先流过你,然后才能轮到我MyHttpModule3呢,不行,必须先轮流着来,这样才公平吗。

那么究竟是怎么样的呢,我不想解释,直接看代码

下面是两个自定义的HttpModule,代码一模一样,就是换了个名字

namespace ClassLibrary1
{
    public class MyHttpModule2:IHttpModule
    {

        public void Dispose()
        {
            throw new NotImplementedException();
        }
        public void Init(HttpApplication context)
        {
           context.BeginRequest+=new EventHandler(context_BeginRequest);
           context.AuthenticateRequest+=new EventHandler(context_AuthenticateRequest);
           context.AuthorizeRequest+=new EventHandler(context_AuthorizeRequest);
           context.ResolveRequestCache+=new EventHandler(context_ResolveRequestCache);
           context.AcquireRequestState+=new EventHandler(context_AcquireRequestState);
           context.PreRequestHandlerExecute+=new EventHandler(context_PreRequestHandlerExecute);
           context.PostRequestHandlerExecute+=new EventHandler(context_PostRequestHandlerExecute);
           context.ReleaseRequestState+=new EventHandler(context_ReleaseRequestState);
           context.UpdateRequestCache+=new EventHandler(context_UpdateRequestCache);
           context.EndRequest+=new EventHandler(context_EndRequest);
           context.PreSendRequestHeaders+=new EventHandler(context_PreSendRequestHeaders);
           context.PreSendRequestContent+=new EventHandler(context_PreSendRequestContent);
        }
        private void context_BeginRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_AuthenticateRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_AuthorizeRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_ResolveRequestCache(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_AcquireRequestState(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_PostRequestHandlerExecute(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_UpdateRequestCache(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            application.Context.Response.Write("MyHttpModule2中的context_UpdateRequestCache<br/>");
        }
        private void context_EndRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_ReleaseRequestState(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_PreSendRequestHeaders(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_PreSendRequestContent(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
         
View Code
namespace ClassLibrary1
{
    public class MyHttpModule3:IHttpModule
    {

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest);
            context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);
            context.ResolveRequestCache += new EventHandler(context_ResolveRequestCache);
            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
            context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute);
            context.ReleaseRequestState += new EventHandler(context_ReleaseRequestState);
            context.UpdateRequestCache += new EventHandler(context_UpdateRequestCache);
            context.EndRequest += new EventHandler(context_EndRequest);
            context.PreSendRequestHeaders += new EventHandler(context_PreSendRequestHeaders);
            context.PreSendRequestContent += new EventHandler(context_PreSendRequestContent);
        }
        private void context_BeginRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_AuthenticateRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_AuthorizeRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_ResolveRequestCache(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_AcquireRequestState(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_PostRequestHandlerExecute(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_UpdateRequestCache(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            application.Context.Response.Write("MyHttpModule3中的context_UpdateRequestCache<br/>");
        }
        private void context_EndRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_ReleaseRequestState(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_PreSendRequestHeaders(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
        private void context_PreSendRequestContent(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

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

        }
    }
}
View Code

下面是要请求的Default.aspx页面

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

运行起来,我们请求Default.aspx页面看到下面的结果:

看到结果我想就不用解释了吧,Asp.Net声明周期中选择的是第二种选择。

3、利用HttpModule实现终止此次HttpRequest请求

如果我们在MyHttpModule2中的context_BeginRequest事件中就终止了此次的Http请求,那么接下来的事件还会继续往下执行吗?

还是上面的两个自定义HttpModule(MyHttpModule2和MyHttpModule3),我把MyHttpModule2中的context_BeginRequest事件改写如下:

 private void context_BeginRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;
            application.CompleteRequest();
            application.Context.Response.Write("此次Http请求已经终止<br/>");

        }

运行结果如下:

写到这里,我想现在客户端发出一个Http请求经过HttpModule内部时到底怎样执行,执行哪些事件,以及我们能够利用这些事件做些什么你们应该有一定的了解了,如果你还没有了解,请给我留言,再往下我们该介绍真正的处理中心HttpHandler这个东东了,如果您喜欢我的文章,请继续关注。

本系列:

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

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

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

 

你可能感兴趣的:(asp.net)