HttpModule with filter - Writer method is called twice

I have an HttpModule with a filter (PageFilter) where the Writer method on the PageFilter is called twice for every page request, unfortunately not with the same result.

The idea behind the filter is to find "" and insert text / script into it. I found a few small bugs (and fixed them), but this bug plays tricks on me ...

Og PageFilter constructor is called once, but its writer is called twice per request?

below is the content of PageFilter.Writer (which is executed 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);
    }

      

The first time the method runs register (1) and the second time (2) runs ... it doesn't justify what I want, does anyone know why and / or how I can get it to work (consistently) ?

+1


source to share


2 answers


The Write method can be called multiple times for the same page. The HttpWriter object concatenates the data and then writes it to the output stream. Each time the HttpWriter sends a chunk of data, the Write response method is called.

Refer to this solution ...



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

I hope you find it helpful.

+1


source


These "events" occur during a single page request:

isAspx = true og / _layouts / not found (I check that the file is .aspx and the url does not contain / _layouts /)

PageFilter constructor called

Created method Writer ...

eof (regex): (regex containing generated to match)

! eof.IsMatch (strBuffer): (no regex match)

Created method Writer ... (second time around calling writer)

eof (regex): (regex containing generated to match)

Regex triggered (matches regex)

re (regex): (found body tag I need to insert my script)

ScriptInclude = true (I found the web.config key tells my application to include the script)

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



The problem is when deploying my dev, the script is run twice, causing the above sequence and script to be included. In my test deployment, the script runs twice and ends with a NOT including script ...

I would like to avoid calling Writer twice, but furthermore, I would like the script to be included in the test deployment

0


source







All Articles