带过滤器的 HttpModule - Writer 方法被调用两次

发布于 2024-07-09 11:30:18 字数 1236 浏览 5 评论 0原文

我有一个带有过滤器(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2024-07-16 11:30:18

对于单个页面,Write 方法可能会被多次调用。 HttpWriter 对象将数据分块在一起,然后将其写入其输出流。 每次 HttpWriter 发出一块数据时,都会调用响应过滤器的 Write 方法。

请参阅此解决方案...

  1. HttpResponse.Filter写入多次

而不是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...

  1. HttpResponse.Filter write multiple times

Instead of responseStream.Write(data, 0, data.Length);
try responseStream.Write(data, 0, data.Length-1);

Hope you find this useful.

爱人如己 2024-07-16 11:30:18

这些“事件”发生在 1 页请求期间:

isAspx = true og /_layouts/ 未找到
(我验证该文件是 .aspx 并且
网址不包含 /_layouts/)

调用 PageFilter 构造函数

编写器方法已启动...

eof(正则表达式):(正则表达式包含
为匹配而创建)

!eof.IsMatch(strBuffer): (没有
匹配正则表达式)

Writer 方法已启动...(第二个
打电话给作者的时间)

eof(正则表达式):(正则表达式包含
为匹配而创建)

启动正则表达式(与正则表达式匹配)

re(正则表达式):(找到正文
插入脚本所需的标签)

ScriptInclude = true(我发现
web.config 键告诉我的应用程序它
应包含脚本)

使用美国脚本(我使用了美国脚本
该脚本的版本也基于
网络配置键)

问题是:在我的开发部署中,编写器运行两次,最终得到上述序列并包含脚本。 在我的测试部署中,编写器运行两次,最终不包含脚本...

我想避免调用编写器两次,但更希望将脚本包含在测试部署中

These "events" happens during 1 page request:

isAspx = true og /_layouts/ not found
(I verify that the file is .aspx and
URL not containing /_layouts/)

PageFilter constructor called

Writer method initiated...

eof (regex): (regex containing
created for matching)

!eof.IsMatch(strBuffer): (did not
match the regex )

Writer method initiated... (second
time around callinge the writer)

eof (regex): (regex containing
created for matching)

Regex initiated (matched the regex)

re (regex): (found the body
tag I need for inserting my script)

ScriptInclude = true (I've found the
web.config key telling my app that it
should include the script)

US script used (I have used the US
version of the script also based on a
web config key)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文