ASP.NET MVC自定义Module记录管道事件执行顺序

1. 在Visual Studio 新建项目,模板为空,下面结构选择MVC。

2. 在项目中新建一个类MyModule,实现IHttpModule接口

namespace SimpleApp.Infrastructure
{
    /// 
    /// 记录请求管道事件执行顺序
    /// 
    public class MyModule : IHttpModule
    {
        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            //下面是记录事件的执行
            //提供自定义日志记录实现的示例
            context.BeginRequest += (src, args) => RecordEvent(context);

            context.AuthenticateRequest += (src, args) => RecordEvent(context);
            context.PostAuthenticateRequest += (src, args) => RecordEvent(context);

            context.AuthorizeRequest += (src, args) => RecordEvent(context);
            context.PostAuthorizeRequest += (src, args) => RecordEvent(context);

            context.ResolveRequestCache += (src, args) => RecordEvent(context);
            context.PostResolveRequestCache += (src, args) => RecordEvent(context);

            //MapRequestHandler:仅支持IIS7以上,Net Framwork3.0以上的集成模式
            context.MapRequestHandler += (src, args) => RecordEvent(context);
            context.PostMapRequestHandler += (src, args) => RecordEvent(context);

            context.AcquireRequestState += (src, args) => RecordEvent(context);
            context.PostAcquireRequestState += (src, args) => RecordEvent(context);


            context.PreRequestHandlerExecute += (src, args) => RecordEvent(context);
            context.PostRequestHandlerExecute += (src, args) => RecordEvent(context);

            context.ReleaseRequestState += (src, args) => RecordEvent(context);
            context.PostReleaseRequestState += (src, args) => RecordEvent(context);

            context.UpdateRequestCache += (src, args) => RecordEvent(context);
            context.PostUpdateRequestCache += (src, args) => RecordEvent(context);

            //LogRequest、PostLogRequest支持IIS7以上,Net Framwork3.0以上的集成模式
            context.LogRequest += (src, args) => RecordEvent(context);
            context.PostLogRequest += (src, args) => RecordEvent(context);

            context.EndRequest += (src, args) => RecordEvent(context);

            context.PreSendRequestHeaders += (src, args) => RecordEvent(context);
            context.PreSendRequestContent += (src, args) => RecordEvent(context);

            context.Error += (src, args) => RecordEvent(context);
            context.RequestCompleted += (src, args) => RecordEvent(context);

            context.Disposed += (src, args) => RecordEvent(context);
        }

        private static int index = 0;
        private void RecordEvent(HttpApplication context)
        {
            index++;
            string eventName = HttpContext.Current.CurrentNotification.ToString();
            if (eventName== "BeginRequest")
            {
                index = 1;
            }
            if (HttpContext.Current.IsPostNotification)
            {
                eventName = "Post" + eventName;
            }
            using (StreamWriter sw = new StreamWriter(@"d:\asplog.txt",true,Encoding.UTF8))
            {
                sw.WriteLine("=====================================");
                sw.WriteLine(index+": "+eventName);
                sw.WriteLine("=====================================");
                sw.Flush();
            }
        }
    }


}

3. 在App_Start文件夹中,定义类ModuleRegistration,注册MyModule

using System.Web;

[assembly: PreApplicationStartMethod(typeof(SimpleApp.ModuleRegistration), "RegisterModule")]
namespace SimpleApp
{
    public class ModuleRegistration
    {
        public static void RegisterModule()
        {
            HttpApplication.RegisterModule(typeof(SimpleApp.Infrastructure.MyModule));
        }
    }
}

4. 新建HomeController,并新建Index视图

5. ctrl+F5运行项目,然后关闭,在d盘可以看懂asplog.txt记录的请求管道中事件的执行顺序

ASP.NET MVC自定义Module记录管道事件执行顺序_第1张图片

 

 

你可能感兴趣的:(ASP.NET MVC自定义Module记录管道事件执行顺序)