为此,您必须实现自己的流对象并将其用作响应的过滤器。
例如:
public class TimerStream : Stream
{
private Stream inner { get; set; }
private StringBuilder responseHtml;
public TimerStream(Stream inputStream) {
inner = inputStream;
responseHtml = new StringBuilder();
// Setup your timer
}
/* Filter overrides should pass through to inner, all but Write */
public override void Write(byte[] buffer, int offset, int count)
{
string bufferedHtml = System.Text.UTF8Encoding.UTF8.GetString (buffer, offset, count);
Regex endTag = new Regex ("</html>", RegexOptions.IgnoreCase);
if (!endTag.IsMatch (bufferedHtml))
{
responseHtml.Append(bufferedHtml);
}
else
{
// insert timer html into buffer, then...
responseHtml.Append (bufferedHtml);
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes (responseHtml.ToString ());
inner.Write (data, 0, data.Length);
}
}
}
然后,在您的 HttpModule 中,将其添加到您的 BeginRequest 中:
// Change the Stream filter
HttpResponse response = context.Response;
response.Filter = new TimerStream(context.Response.Filter);