asp.net的事件处理

Global.asax与httpModule

  • 共同点
    两者创建的应用程序对象都能提供引用程序的全局访问点,两者都可以存储请求期间的全局状态和相应应用程序范围事件。

  • 区别
    Global只能伴随着应用程序,旨在管理特定应用程序的状态和事件。
    HttpModule模块是完全独立的程序集,不必与特定的应用程序绑定,更适合实现多个应用程序通用的功能

Global.asax 实现HttpApplication的重写

通过gloabal中的全局事件可以方便的管理应用程序中的数据和事件。
创建Global.asax文件,文件中会有相应的事件处理程序

      protected void Application_Start(object sender, EventArgs e)
        {
           
        }

        protected void Session_Start(object sender, EventArgs e)
        {
           
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
           HttpContext context=HttpContext.Current;
            context.Items["begin"] = DateTime.Now;

        }
        protected void Application_EndRequest(object sender, EventArgs e)
        {
           HttpContext context=HttpContext.Current;
            TimeSpan ts = DateTime.Now - (DateTime) context.Items["begin"];
          

        }
        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)
        {
        }
  1. Application_Start
    在应用程序初始化时运行,即第一个请求到达时,在整个应用程序生命周期开始时运行,只运行一次。可以加载和初始化数据。
  2. Application_End
    在关闭应用程序时运行,可以清理需要特别释放的资源
  3. Application_Error
    未处理的异常,可以在这里进行处理
  4. Application_BeginRequest
    每次用户发起请求时都会被触发,注意:请求静态资源并不会触发。
  5. Session_Start
    在每个用户最初请求应用程序时被触发
  6. Session_End
    在会话被释放时触发。

HttpModule

也可以通过HTTP模块管理应用程序的数据和事件。
将httpModule融入到应用程序

  1. 编写实现IHttpModule接口的实现类
  2. 编写相关事件的处理程序
  3. 订阅事件
  4. 在web.config中配置模块
    1-3代码:

namespace TimingModuleLIb
{
    public class Timer:IHttpModule
    {
        public void Dispose()
        {
            
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += OnBeginReq;
            context.EndRequest += OnEndReq;
        }

        public void OnBeginReq(object sender,EventArgs e)
        {

            HttpContext context = HttpContext.Current;
            context.Items["begin"] = DateTime.Now;
        }

        public void OnEndReq(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;
            TimeSpan ts = DateTime.Now - (DateTime)context.Items["begin"];
            context.Response.Write(ts);
        }
    }
}

配置文件,IIS7

 <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      <add name="timingModule" type="TimingModuleLIb.Timer"/>
    </modules>
  
  </system.webServer>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值