我有一些旧网站,每个网站都有很多静态 HTML 页面。我想使用 IIS 模块来捕获生成的页面内容并添加额外的 HTML 片段以使其具有新的页眉和页脚(这称为装饰器模式)。这是我的模块代码。奇怪的是,在许多测试中,我注意到该模块被调用了两次当页面被加载并且每次调用将页面的部分内容传递给模块时(第一次调用传递页面的顶部,第二次调用传递页面的剩余部分)。我知道模块被调用两次的原因是因为我使用了一个静态变量来捕获调用次数并将其显示在新的页眉和页脚中(两个数字不同,页脚编号始终比页眉编号大 1)。我还能够将页面内容导出到两个不同的文件中来证明这一点。
namespace MyProject
{
public class MyModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication application)
{
application.ReleaseRequestState += new EventHandler(this.My_Wrapper);
}
public String ModuleName
{
get { return "MyProject"; }
}
public void My_Wrapper(Object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpContext context = app.Context;
HttpRequest request = context.Request;
string requestPath = request.Path.ToString();
//I have guarding code here so that the following code only applies to
//web requests that has ".html" in the end.
HttpContext.Current.Response.Filter = new WrapperFilter(HttpContext.Current.Response.Filter);
}
}
public class WrapperFilter : MemoryStream
{
private static Regex startOfBody = new Regex("(?i)<body(([^>])*)>", RegexOptions.Compiled | RegexOptions.Multiline);
private static Regex endOfBody = new Regex("(?i)</body>", RegexOptions.Compiled | RegexOptions.Multiline);
private Stream outputStream = null;
private static int index = 0;
public WrapperFilter(Stream output)
{
outputStream = output;
}
public override void Write(byte[] buffer, int offset, int count)
{
string contentInBuffer = UTF8Encoding.UTF8.GetString(buffer);
string page = new StringBuilder(contentInBuffer).ToString();
byte[] outputBuffer = null;
Match matchStartOfBody = null;
Match matchEndOfBody = null;
index++;
matchStartOfBody = startOfBody.Match(page);
string header = "html snippets for header: " + index;
page = startOfBody.Replace(page, "<body " + matchStartOfBody.Groups[1] + ">" + header);
matchEndOfBody = endOfBody.Match(page);
string footer = "html snippets for footer: " + index;
page = endOfBody.Replace(page, footer + "</body>");
outputBuffer = UTF8Encoding.UTF8.GetBytes(page);
outputStream.Write(outputBuffer, 0, outputBuffer.Length);
}
}
}
问题:
模块加载两次的原因是页面内容太大还是需要增加缓存?如果是这样,怎么做?
从技术上讲,我的方法行得通吗?我能够装饰 HTML 页面,并且由于两个调用过程,我无法处理一些高级情况。
当图像需要显示在浏览器页面中,并且对图像的请求通过 IIS 模块?
更新
根据来自 usr 的宝贵意见,“奇怪”行为只是 IIS 的正常行为。由于他/她的建议,我添加了一个类变量:
private byte[] allContent = new byte[0];
以及以下更新的方法:
public override void Write(byte[] buffer, int offset, int count)
{
//new bigger array
byte[] newArr = new byte[allContent.Length + buffer.Length];
//copy old content
System.Array.Copy(allContent, newArr, allContent.Length);
//append new content
System.Array.Copy(buffer, 0, newArr, allContent.Length, buffer.Length);
//reset current total content
allContent = newArr;
}
并添加一个新方法,其中包含从我之前的 Write 方法中复制的所有代码:
protected override void Dispose(bool disposing)
{
//code copied from my earlier code, with "buffer" changed to "allContent".
}
现在一切正常!谢谢楼主!!!