IHttpModuleとIHttpHandle
HttpRequestライフサークル
★ASP.NETのデフォルトHttpModule
System.Web.SessionState.SessionStateModule
System.Web.Security.WindowsAuthenticationModule
System.Web.Security.FormsAuthenticationModule
System.Web.Security.PassportAuthenticationModule
System.Web.Security.UrlAuthorizationModule
System.Web.Security.FileAuthorizationModule
★IHttpModule
要求により付加関係
ページ処理の前後処理
すべてのリクエストが対象になる
public interface IHttpModule
{
void Dispose();
void Init(HttpApplication context);
}
自作
namespace MyNamespace
{
public class MyHttpModule : IHttpModule
{
public MyHttpModule() { }
public void Init(HttpApplication context)
{
context.BeginRequest +=
new EventHandler(this.Application_BeginRequest);
}
public void Dispose() { }
private void Application_BeginRequest(Object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpResponse response = application.Response;
HttpRequest request = application.Request;
response.Write("xxx");
}
}
}
設定
<httpModules>
<add name="demo"
type="MyNamespace.MyHttpModule,MyNamespace"></add>
</httpModules>
※type:クラス名+dll名
★IHttpHandle
パスにより置換関係
ページ処理
Pathで指定したリクエストが対象になる
public interface IHttpHandler
{
bool IsReusable { get; }
void ProcessRequest(HttpContext context);
}
自作
namespace MyNamespace
{
public class MyHttpHandler:IHttpHandler,IRequiresSessionState
{
public MyHttpHandler() {}
public bool IsReusable
{
get
{
return true;
}
}
public void ProcessRequest(HttpContext context)
{
HttpResponse response = context.Response;
HttpRequest request = context.Request;
HttpSessionState Session = context.Session;
Session["key"] = "xxx";
response.Write(Session["key"]);
}
}
}
※Sessionを利用する場合、IRequiresSessionStateが必要
設定
<httpHandlers>
<add verb="*" path="*.aspx"
type="MyNamespace.MyHttpHandler,MyNamespace"></add>
</httpHandlers>
※verb:GET, POST, *