HttpModule 工作正常(“hello”被替换为“hello world”),但由于某些原因,当模块添加到 Web.config 时,WebForms 上的图像不显示。从 Web.config 中删除模块后,将显示 WebForms 上的图像。
有谁知道为什么?
使用或不使用 HttpModule 生成的 HTML 完全相同!
//The HttpModule
public class MyModule : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
//Empty
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(OnBeginRequest);
application = context;
}
#endregion
void OnBeginRequest(object sender, EventArgs e)
{
application.Response.Filter = new MyStream(application.Response.Filter);
}
}
//过滤器 - 将“hello”替换为“hello world”
public class MyStream : MemoryStream
{
private Stream outputStream = null;
public MyStream(Stream output)
{
outputStream = output;
}
public override void Write(byte[] buffer, int offset, int count)
{
string bufferContent = UTF8Encoding.UTF8.GetString(buffer);
bufferContent = bufferContent.Replace("hello", "hello world");
outputStream.Write(UTF8Encoding.UTF8.GetBytes(bufferContent), offset, UTF8Encoding.UTF8.GetByteCount(bufferContent));
base.Write(buffer, offset, count);
}
}