using System;
using System.Collections.Generic;
using System.Text;
using System.Web; //要在自己的类库中添加System.Web引用
namespace MyHttpModule
{
class MySelfHttpModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(Application_BeginRequest);
application.EndRequest += new EventHandler(Application_EndRequest);
}
// 自己要针对一些事情进行处理的两个方法
private void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpContext context = application.Context;
HttpRequest request = application.Request;
HttpResponse response = application.Response;
response.Write("我来自自定义HttpModule中的BeginRequest<br />");
}
private void Application_EndRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpContext context = application.Context;
HttpRequest request = application.Request;
HttpResponse response = application.Response;
response.Write("我来自自定义HttpModule中的EndRequest<br />");
}
}
}
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<br/>来自Default.aspx页面<br/>");
}
}
<httpModules>
<add name="MySelfHttpModule" type="MyHttpModule.MySelfHttpModule,MyHttpModule"></add>
</httpModules>
</system.web>
<httpModules>
<add name="MySelfHttpModule" type="MyHttpModule.MySelfHttpModule,MyHttpModule"></add>
<add name="MySelfHttpModule2" type="MyHttpModule2.MySelfHttpModule2,MyHttpModule2"></add>
</httpModules>
</system.web>
我来自自定义HttpModule中的BeginRequest
我来自自定义HttpModule2中的BeginRequest
来自Default.aspx页面
我来自自定义HttpModule中的EndRequest
我来自自定义HttpModule2中的EndRequest