Web アプリケーションが開始された後、Web リクエスト中に、あるアクションを 1 回または 2 回だけ実行したいとします。
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);
}
}
次の例外がスローされます。
イベント ハンドラーは、IHttpModule の初期化中にのみ HttpApplication イベントにバインドできます。