1.HttpModule可用在asp.net 管线事件触发的过程中。。 可处理一些通用的操作,如给特定请求加 gzip压缩。
2.示例代码:
using System;
using System.Web; namespace MyWebApp
{
public class MyHttpModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(application_BeginRequest);
} public void application_BeginRequest(object sender, EventArgs e)
{
HttpContext context = (sender as HttpApplication).Context;
context.Response.Write("这一部分是由HttpModule添加!<br><script>alert('测试脚本标签')</script>");
} #region IHttpModule 成员 void IHttpModule.Dispose()
{
throw new NotImplementedException();
} #endregion
}
}
3.要使 HttpModule生效。还需要配置web.config。
<system.webServer>
<modules>
<remove name="MyHttpModule"/>
<add name="MyHttpModule" type="MyWebApp.MyHttpModule"/>
</modules>
</system.webServer>