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)?
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.