浅谈Httpmodules

HttpModule是ASP.NET过滤器,可以理解为HTTP请求的必经之地
我们只要实现IHttpModule接口,就可以取代HttpModule

namespace BookShop.Handler
{
public class TestModule : IHttpModule
{
/// <summary>
/// 您将需要在网站的 Web.config 文件中配置此模块
/// 并向 IIS 注册它,然后才能使用它。有关详细信息,
/// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
/// </summary>
#region IHttpModule Members

public void Dispose()
{
//此处放置清除代码。
}

public void Init(HttpApplication context)
{
// 下面是如何处理 LogRequest 事件并为其
// 提供自定义日志记录实现的示例
context.LogRequest += new EventHandler(OnLogRequest);
context.BeginRequest += Context_BeginRequest;
context.EndRequest += Context_EndRequest;
}

private void Context_EndRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
app.Context.Response.Write("执行了HttpMoudle的endRequest事件");
}

private void Context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
app.Context.Response.Write("执行了HttpMoudle的BeginRequest事件");

}

#endregion

public void OnLogRequest(Object source, EventArgs e)
{
HttpApplication app = source as HttpApplication;
app.Context.Response.Write("执行了HttpMoudle的LogRequest事件");
}
}
}


web.config
<httpModules>
<add name="myHttpModule" type="ClassLibrary1.MyHttpModule,ClassLibrary1"/>
</httpModules>

原文地址:https://www.cnblogs.com/ZaraNet/p/9433533.html