9

Global.asaxのPostAuthenticateRequestイベントを使用するにはどうすればよいですか?私はこのチュートリアルに従っていますが、 PostAuthenticateRequestイベントを使用する必要があると記載されています。Global.asaxイベントを追加すると、マークアップファイルと分離コードファイルの2つのファイルが作成されました。コードビハインドファイルの内容は次のとおりです

using System;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace authentication
{
    public class Global : System.Web.HttpApplication
    {    
        protected void Application_Start(object sender, EventArgs e)
        {    
        }

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

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

        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)
        {    
        }
    }
}

今私が入力すると

protected void Application_OnPostAuthenticateRequest(object sender, EventArgs e)

正常に呼び出されます。ここで、 PostAuthenticateRequestがこのApplication_OnPostAuthenticateRequestメソッドにどのようにバインドされているかを知りたいですか?メソッドを他の方法に変更するにはどうすればよいですか?

4

1 に答える 1

16

マジック...、オートイベントワイヤーアップと呼ばれるメカニズム、あなたが書くことができるのと同じ理由

Page_Load(object sender, EventArgs e) 
{ 
} 

コードビハインドでは、ページが読み込まれるときにメソッドが自動的に呼び出されます。

System.Web.Configuration.PagesSection.AutoEventWireupプロパティのMSDNの説明

ASP.NETページのイベントがイベント処理関数に自動的に接続されるかどうかを示す値を取得または設定します。

の場合、ハンドラーAutoEventWireuptrue実行時に名前と署名に基づいてイベントに自動的にバインドされます。ASP.NETは、イベントごとに、またはPage_eventname()などのパターンに従って名前が付けられたメソッドを検索します。ASP.NETは、最初に、一般的なイベントハンドラーシグネチャ(つまり、パラメーターを指定する)を持つオーバーロードを探します。この署名を持つイベントハンドラーが見つからない場合、ASP.NETはパラメーターのないオーバーロードを探します。この回答の詳細。Page_Load()Page_Init()ObjectEventArgs

明示的に実行したい場合は、代わりに次のように記述します

public override void Init()
{
    this.PostAuthenticateRequest +=
        new EventHandler(MyOnPostAuthenticateRequestHandler);
    base.Init();
}

private void MyOnPostAuthenticateRequestHandler(object sender, EventArgs e)
{
}
于 2011-01-13T07:59:06.840 に答える