带过滤器的 HttpModule - Writer 方法被调用两次
我有一个带有过滤器(PageFilter)的 HttpModule,其中 PageFilter 的 Writer 方法针对每个页面请求被调用两次,不幸的是结果不同。
过滤器的想法是找到“”并在其前面插入一些文本/脚本。 我找到了一堆小错误(并纠正了它们),但是这个错误在欺骗我......
构造函数 og PageFilter 被调用一次,但它的 writer 方法每个请求被调用两次?
下面是 PageFilter.Writer 的内容(运行两次)
string strBuffer = System.Text.UTF8Encoding.UTF8.GetString (buffer, offset, count);
try
{
Regex eof = new Regex("</html>", RegexOptions.IgnoreCase);
if (!eof.IsMatch(strBuffer))
{
//(1)
responseHtml.Append(strBuffer);
}
else
{
//(2)
responseHtml.Append (strBuffer);
string finalHtml = responseHtml.ToString ();
Regex re = null;
re = new Regex ("</body>", RegexOptions.IgnoreCase);
finalHtml = re.Replace(finalHtml, new MatchEvaluator(lastWebTrendsTagMatch));
// Write the formatted HTML back
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes (finalHtml);
responseStream.Write(data, 0, data.Length);
}
}
catch (Exception ex)
{
Logging.Logger(Logging.Level.Error, "Failed writing the HTML...", ex);
}
该方法第一次运行案例 (1) 运行,第二次案例 (2) 运行...这不是我想要的,任何人都知道为什么和/或我如何可以让它工作(一致)吗?
I have a HttpModule with a filter (PageFilter) where the Writer method of PageFilter is called twice for every page request, unfortunately not with the same result.
The idea of the filter is to locate "" and insert some text/script in front of this. I have located a bunch of minor errors (and corrected them), but this error is playing tricks on me...
The constructor og PageFilter is called once, but its writer method is called twice per request?
below is the content of PageFilter.Writer (which runs twice)
string strBuffer = System.Text.UTF8Encoding.UTF8.GetString (buffer, offset, count);
try
{
Regex eof = new Regex("</html>", RegexOptions.IgnoreCase);
if (!eof.IsMatch(strBuffer))
{
//(1)
responseHtml.Append(strBuffer);
}
else
{
//(2)
responseHtml.Append (strBuffer);
string finalHtml = responseHtml.ToString ();
Regex re = null;
re = new Regex ("</body>", RegexOptions.IgnoreCase);
finalHtml = re.Replace(finalHtml, new MatchEvaluator(lastWebTrendsTagMatch));
// Write the formatted HTML back
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes (finalHtml);
responseStream.Write(data, 0, data.Length);
}
}
catch (Exception ex)
{
Logging.Logger(Logging.Level.Error, "Failed writing the HTML...", ex);
}
First time the method runs case (1) runs and on 2nd case (2) runs... this is not excatly what I want, anyone knows why and/or how I can make it work (consistently)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于单个页面,Write 方法可能会被多次调用。 HttpWriter 对象将数据分块在一起,然后将其写入其输出流。 每次 HttpWriter 发出一块数据时,都会调用响应过滤器的 Write 方法。
请参阅此解决方案...
而不是responseStream.Write(data, 0, data.Length);
尝试responseStream.Write(data, 0, data.Length-1);
希望你觉得这个有用。
The Write method may be called multiple times for a single page. The HttpWriter object chunks together data and then writes it to its output stream. Each time the HttpWriter sends out a chunk of data your response filter's Write method is invoked.
Refer this for one kind of solution...
Instead of responseStream.Write(data, 0, data.Length);
try responseStream.Write(data, 0, data.Length-1);
Hope you find this useful.
这些“事件”发生在 1 页请求期间:
问题是:在我的开发部署中,编写器运行两次,最终得到上述序列并包含脚本。 在我的测试部署中,编写器运行两次,最终不包含脚本...
我想避免调用编写器两次,但更希望将脚本包含在测试部署中
These "events" happens during 1 page request:
Problem is: on my dev deployment the writer runs twice, ending up with the above sequence and the script being included. On my Test deployment the writer runs twice and ends up NOT including the script...
I would like to avoid the calling of the Writer twice, but more so like to have the script included on test deployment