ASP.NET IHttpModule

4162 ワード

IHttpModuleインタフェースの定義:実装クラスにモジュール初期化および処理イベントを提供します.Dispose()とInit()の2つの方法が含まれています.
カスタムIHttpModuleインタフェース
カスタムIHttpModuleインタフェースを実装するには、次の2つのステップが必要です。 1)IHttpModuleインタフェースを継承したクラスを実現する 2)Web.configファイルにこのカスタムHttpModuleを登録 public class CustomerModule:IHttpModule { #region IHttpModuleメンバー public void Dispose() { } public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); context.EndRequest += new EventHandler(context_EndRequest); } void context_BeginRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write(「カスタムModuleRequest開始」); } void context_EndRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write(「カスタムModuleRequest終了」); } #endregion }   web.config <httpModules> <add name="customerModule" type="WebApplication3.CustomerModule, WebApplication3"/> </httpModules>

ASP.NET内蔵HttpModule
C:WindowsMicrosoft.NET\Framework\v2.0.50727\CONFIG\web.configの下にはASPがたくさんあります。NET内蔵のHttpMoudleはデフォルトでロードされています。ロードしたくない場合は、Removeを削除してパフォーマンスを向上させることができます。 <remove name="WindowsAuthentication"/> 内蔵は次のとおりです。 <httpModules> <add name="OutputCache" type="System.Web.Caching.OutputCacheModule"/> <add name="Session" type="System.Web.SessionState.SessionStateModule"/> <add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule"/> <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule"/> <add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule"/> <add name="RoleManager" type="System.Web.Security.RoleManagerModule"/> <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule"/> <add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule"/> <add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule"/> <add name="Profile" type="System.Web.Profile.ProfileModule"/> <add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> <add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> </httpModules>

Global.asaxとHttpModule
ASP.NETでGlobal.Asaxは、アプリケーションとSessionイベントを登録したり、HttpModuleに露出したイベント(HttpModuleとカスタムHttpModuleを含む)を登録したりするグローバルファイルです。2つの注意点があります。 1)Http Requestが到着するたびに、アプリケーションイベントはすべて出発しますが、Application_StartとApplication_Endは、最初のリソースファイルがアクセスされたときにのみトリガーされます。 2)HttpModuleはSessionイベントを登録して応答することができず、SessionイベントはGlobalのみである.asaxに登録 public class CustomerModule:IHttpModule { //露出したイベントのカスタマイズ public event EventHandler ExposedEvent; #region IHttpModuleメンバー public void Dispose() { } public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); context.EndRequest += new EventHandler(context_EndRequest); } void context_BeginRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write(「カスタムModuleRequest開始」); } void context_EndRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write(「カスタムModuleRequest終了」); //トリガーイベント OnExposedEvent(new EventArgs()); } #endregion protected void OnExposedEvent( EventArgs e) { if (ExposedEvent!=null) { ExposedEvent(this, e); } } }   Globalでasaxにこのようなコードを追加 protected void customerModule_ExposedEvent(object sender, EventArgs e) { Response.Write(「Global.asaxから」); }   そのうちGlobal.asaxにおけるイベント名の定義は、モジュール名_イベント名。モジュール名はあなたがWeb.configに登録されているモジュール名