首页 > Web开发 > 详细

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

时间:2017-10-12 17:43:55      阅读:312      评论:0      收藏:0      [点我收藏+]

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"); } } }

 

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

原文:http://www.cnblogs.com/mibing/p/7657067.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!