views:

680

answers:

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("", 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)?

+1  A: 

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.

rajesh pillai
Haven't got access to ExpertExchange yet but I'll take a look at the links
noesgard
This one is not a paid link. No subscription required. Just go to the bottom of the page there...
rajesh pillai
yes it is, have to initiate 7 day trial firstto view solution and to do so requirering giving up credit card information. I'm considering if that's ok (dont have any credit cards available at this location, so would have to go collect one... just for testing if solutions i usefull...
noesgard
Even now I am not able to find it. I just browsed it some time back.Remove -1 from length ... responseStream.Write(data, 0, data.Length-1); instead of length;It may be a different case but thought of putting this down.I'll remove this ee URL as this requires paid subscription now....
rajesh pillai
the change of -1 doesn't help - still gets 2 writes on two different pages
noesgard
you're right on the fact that the writer gets called several times, when outputting html. Can't avoit having this behaviour but it's also working now - since i found the "missing link" alias a key on the test platform
noesgard
A: 

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

noesgard
My mistake, a missing key on the test platform caued the malfunction...
noesgard