HttpApplication 事件

Public eventAcquireRequestState当 ASP.NET 获取与当前请求关联的当前状态(如会话状态)时发生。
Public eventAuthenticateRequest当安全模块已建立用户标识时发生。
Public eventAuthorizeRequest当安全模块已验证用户授权时发生。
Public eventBeginRequest在 ASP.NET 响应请求时作为 HTTP 执行管线链中的第一个事件发生。
Public eventDisposed添加事件处理程序以侦听应用程序上的 Disposed 事件。
Public eventEndRequest在 ASP.NET 响应请求时作为 HTTP 执行管线链中的最后一个事件发生。
Public eventError当引发未处理的异常时发生。
Public eventPostAcquireRequestState在已获得与当前请求关联的请求状态(例如会话状态)时发生。
Public eventPostAuthenticateRequest当安全模块已建立用户标识时发生。
Public eventPostAuthorizeRequest在当前请求的用户已获授权时发生。
Public eventPostMapRequestHandler在 ASP.NET 已将当前请求映射到相应的事件处理程序时发生。
Public eventPostReleaseRequestState在 ASP.NET 已完成所有请求事件处理程序的执行并且请求状态数据已存储时发生。
Public eventPostRequestHandlerExecute在 ASP.NET 事件处理程序(例如,某页或某个 XML Web service)执行完毕时发生。
Public eventPostResolveRequestCache在 ASP.NET 跳过当前事件处理程序的执行并允许缓存模块满足来自缓存的请求时发生。
Public eventPostUpdateRequestCache在 ASP.NET 完成了缓存模块的更新并存储了以下响应时发生,这些响应用于满足来自缓存的后续请求。
Public eventPreRequestHandlerExecute恰好在 ASP.NET 开始执行事件处理程序(例如,某页或某个 XML Web service)前发生。
Public eventPreSendRequestContent恰好在 ASP.NET 向客户端发送内容之前发生。
Public eventPreSendRequestHeaders恰好在 ASP.NET 向客户端发送 HTTP 标头之前发生。
Public eventReleaseRequestState在 ASP.NET 执行完所有请求事件处理程序后发生。该事件将使状态模块保存当前状态数据。
Public eventResolveRequestCache当 ASP.NET 完成授权事件以使缓存模块从缓存中为请求提供服务时发生,从而跳过事件处理程序(例如某个页或 XML Web services)的执行。
Public eventUpdateRequestCache当 ASP.NET 执行完事件处理程序以使缓存模块存储将用于从缓存为后续请求提供服务的响应时发生。

Http Request在整个HttpModule中的生命周期图:

 Http Request开始
|
    HttpModule
   |
  HttpModule.BeginRequest()
|
   HttpModule.AuthenticateRequest()
|
   HttpModule.AuthorizeRequest()
|
HttpModule.ResolveRequestCache()
|
 建立HttpHandler控制点
|
   接着处理(HttpHandler已经建立,此后Session可用)
|
HttpModule.AcquireRequestState()
|
   HttpModule.PreRequestHandlerExecute()
|
进入HttpHandler处理HttpRequest
|
 HttpHandler.ProcessRequest()
|
返回到HttpModule接着处理(HttpHandler生命周期结束,Session失效)
|
   HttpModule.PostRequestHandlerExecute()
|
   HttpModule.ReleaseRequestState()
|
   HttpModule.UpdateRequestCache()
|
 HttpModule.EndRequest()
|
   HttpModule.PreSendRequestHeaders()
|
   HttpModule.PreSendRequestContent()
|
 将处理后的数据返回客户端
|
整个Http Request处理结束

示例:

using System;
using System.IO;
using System.Web;
using System.Xml;
using System.Data;
using System.Threading;
using System.Diagnostics;

namespace AbcMis.Framework.OUP.Passport
{
    class AuthenticateModule:System.Web.IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
            application.EndRequest += (new EventHandler(this.Application_EndRequest));
            application.PreRequestHandlerExecute += (new EventHandler(this.Application_PreRequestHandlerExecute));
            application.PostRequestHandlerExecute += (new EventHandler(this.Application_PostRequestHandlerExecute));
            application.ReleaseRequestState += (new EventHandler(this.Application_ReleaseRequestState));
            application.AcquireRequestState += (new EventHandler(this.Application_AcquireRequestState));
            application.AuthenticateRequest += (new EventHandler(this.Application_AuthenticateRequest));
            application.AuthorizeRequest += (new EventHandler(this.Application_AuthorizeRequest));
            application.ResolveRequestCache += (new EventHandler(this.Application_ResolveRequestCache));
            application.PreSendRequestHeaders += (new EventHandler(this.Application_PreSendRequestHeaders));
            application.PreSendRequestContent += (new EventHandler(this.Application_PreSendRequestContent));
        }

        private void Application_PreRequestHandlerExecute(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            string UserGuid = (string)context.Session["UserGuid"];
            context.Response.Write("Application_PreRequestHandlerExecute"+UserGuid+"<br>");
        }

        private void Application_BeginRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;

            context.Response.Write("Application_BeginRequest<br>");
        }

        private void Application_EndRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;

            context.Response.Write("Application_EndRequest<br>");

        }

        private void Application_PostRequestHandlerExecute(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;

            context.Response.Write("Application_PostRequestHandlerExecute<br>");

        }

        private void Application_ReleaseRequestState(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;

            context.Response.Write("Application_ReleaseRequestState<br>");

        }

        private void Application_UpdateRequestCache(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;

            context.Response.Write("Application_UpdateRequestCache<br>");

        }

        private void Application_AuthenticateRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;

            context.Response.Write("Application_AuthenticateRequest<br>");

        }

        private void Application_AuthorizeRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;

            context.Response.Write("Application_AuthorizeRequest<br>");

        }

        private void Application_ResolveRequestCache(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;

            context.Response.Write("Application_ResolveRequestCache<br>");

        }

        private void Application_AcquireRequestState(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;

            context.Response.Write("Application_AcquireRequestState<br>");

        }

        private void Application_PreSendRequestHeaders(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;

            context.Response.Write("Application_PreSendRequestHeaders<br>");

        }

        private void Application_PreSendRequestContent(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;

            context.Response.Write("Application_PreSendRequestContent<br>");

        }
        public void Dispose()
        {
        }

    }

}

注:本文是网上转贴

转载请注明出处[ http://samlin.cnblogs.com/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值