ASP.NET Response Filter to Reformat the rendered output of ASPX pages?

Posted by PropellerHead on Stack Overflow See other posts from Stack Overflow or by PropellerHead
Published on 2009-10-10T08:14:27Z Indexed on 2014/06/12 15:25 UTC
Read the original article Hit count: 235

Filed under:
|
|
|
|

I've created a simple HttpModule and response stream to reformat the rendered output of web pages (see code snippets below).

In the HttpModule I set the Response.Filter to my PageStream:

m_Application.Context.Response.Filter = new PageStream(m_Application.Context);

In the PageStream I overwrite the Write method in order to do my reformatting of the rendered output:

public override void Write(byte[] buffer, int offset, int count)
{
    string html = System.Text.Encoding.UTF8.GetString(buffer);
    //Do some string resplace operations here...
    byte[] input = System.Text.Encoding.UTF8.GetBytes(html);
    m_DefaultStream.Write(input, 0, input.Length);
}

And this work fine when using it on simple HTML pages (.html), but when I use this method on ASPX pages (.aspx), the Write method is called several times, splitting up the reformatting into different steps, and potentially destroying the string replacement operations.

How do I solve this? Is there a way to let the ASPX page NOT call Write several times, e.g. by changing its buffer size, or have I chosen the wrong approach entirely, by using this Response.Filter method to manipulate the rendered output?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about filter