ASP.NET Response Filter to Reformat the rendered output of ASPX pages?
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 i开发者_运维问答s 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?
I would just buffer the data in the Write method, and do the string operations in the Close method, like this:
private readonly Stream _forwardStream;
private readonly StringBuilder _sb;
// snip
public override void Write (byte[] buffer, int offset, int count)
{
string chunk = Encoding.UTF8.GetString (buffer, offset, count);
_sb.Append (chunk);
}
public override void Close ()
{
string result = GetManipulatedString ();
byte[] rawResult = Encoding.UTF8.GetBytes (result);
_forwardStream.Write (rawResult, 0, rawResult.Length);
base.Close ();
_forwardStream.Close ();
}
(Maybe even better if you collect the data in a MemoryStream)
Guided by the article suggested by Darin Dimitrov, I ended with the following implementation of the Write method that also works perfectly with ASPX pages:
public override void Write(byte[] buffer, int offset, int count)
{
string strBuffer = System.Text.UTF8Encoding.UTF8.GetString (buffer, offset, count);
if (!strBuffer.Contains("</html>"))
{
m_ResponseHtml.Append(strBuffer);
}
else
{
m_ResponseHtml.Append(strBuffer);
string html = m_ResponseHtml.ToString ();
//Do some string operations here...
byte[] input = System.Text.Encoding.UTF8.GetBytes(html);
m_DefaultStream.Write(input, 0, input.Length);
}
}
The code leverages a StringBuilder (m_ResponseHtml) to accumulate the entire HTML, before doing the actual string manupulation on the rendered output.
You might need to check the content type before attaching the response filter:
var response = m_Application.Context.Response;
if (response.ContentType == "text/html")
{
response.Filter = new PageStream(m_Application.Context);
}
There's also a nice article describing response filters in ASP.NET.
PropellerHead's response relies on finding the closing html tag in the final buffer, but I actually had the final buffer be too small to contain the entire tag.
A safer (and more efficient as well) method is to only do the append in Write and to then do the string operations and output in Close.
精彩评论