如何确定asp.net请求生命周期的当前处理事件

1 首先在全局应用程序里面添加如下代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace Events
{
    public class Global : System.Web.HttpApplication
    {

        public Global()
        {
            BeginRequest += HandleEvent;
            EndRequest += HandleEvent;
            AcquireRequestState += HandleEvent;
            PostAcquireRequestState += HandleEvent;
        }

        private void HandleEvent(object sender, EventArgs e)
        {
            //httpContext类用于访问所有有关应用程序,所处理的请求以及正在构建的请求的信息,并且它的属性currentNotification定义了HttpApplication
事件的子集
string eventName = "<Unknown>"; switch (Context.CurrentNotification) { case RequestNotification.BeginRequest: case RequestNotification.EndRequest: eventName = Context.CurrentNotification.ToString(); break; case RequestNotification.AuthenticateRequest: break; case RequestNotification.AuthorizeRequest: break; case RequestNotification.ResolveRequestCache: break; case RequestNotification.MapRequestHandler: break; //当asp.net fromework需要与请求关联的状态时,将触发改事件 case RequestNotification.AcquireRequestState: if (Context.IsPostNotification) { eventName = "PostAcquireRequestState"; } else { eventName = "AcquireRequestState"; } break; case RequestNotification.PreExecuteRequestHandler: break; case RequestNotification.ExecuteRequestHandler: break; case RequestNotification.ReleaseRequestState: break; case RequestNotification.UpdateRequestCache: break; case RequestNotification.LogRequest: break; case RequestNotification.SendResponse: break; default: break; } EventCollection.Add(EventSource.Application, eventName); } protected void Application_Start(object sender, EventArgs e) { EventCollection.Add(EventSource.Application, "Start"); Application["message"] = "Aplication Events"; } protected void Session_Start(object sender, EventArgs e) { } protected void Application_BeginRequest(object sender, EventArgs e) { //收到请求时触发的第一个事件 EventCollection.Add(EventSource.Application, "BeginRequest"); Response.Write(string.Format("request started at {0}", DateTime.Now.ToLongTimeString())); } protected void Application_EndRequest(object sender,EventArgs e) { EventCollection.Add(EventSource.Application, "EndRequest"); } protected void Application_AuthenticateRequest(object sender, EventArgs e) { } protected void Application_Error(object sender, EventArgs e) { } protected void Session_End(object sender, EventArgs e) { } protected void Application_End(object sender, EventArgs e) { EventCollection.Add(EventSource.Application,"End"); } } }
原文地址:https://www.cnblogs.com/mibing/p/7657067.html