【发布时间】:2011-04-29 20:35:05
【问题描述】:
假设我们只想在 Web 应用程序启动后和 Web 请求期间执行某个操作一次或几次。
public class WebApp : HttpApplication
{
public override void Init()
{
base.Init();
this.BeginRequest += new EventHandler(this.OnFirstBeginRequest);
}
private void OnFirstBeginRequest(object sender, EventArgs e)
{
// do some action and if everything is OK, unbind this handler,
// because we need it executed only once at the first web request
this.BeginRequest -= new EventHandler(this.OnFirstBeginRequest);
}
}
会抛出以下异常:
事件处理程序只能绑定到 期间的 HttpApplication 事件 IHttpModule 初始化。
【问题讨论】:
-
你为什么不用
Global.asax和Application_Start? -
赛克龙,我为什么要这样做?这会使代码示例复杂化,将我的问题主题转移到不同的方向。
标签: c# asp.net binding ihttpmodule httpapplication