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

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

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

namespace SimpleApp.Infrastructure
{
    /// <summary>
    /// 记录请求管道事件执行顺序
    /// </summary>
    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记录的请求管道中事件的执行顺序

 

 

转载于:https://www.cnblogs.com/AlexanderZhao/p/10588001.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值