private void Finish(object sender, EventArgs e) { ResponseCaptureStream filter = app.Response.Filter as ResponseCaptureStream; if (filter != null){ //never make it here in v6 string responseText = filter.StreamContent;// this is how I'm grabbing the 'final' output } }
I do it slightly differently, here's some code. I can't remember why I stick the StreamWatcher in the context items, but there was a good reason (probably haha).
private void Start(Object sender,EventArgs e) { var watcher = new StreamWatcher(context.Response.Filter); context.Context.Items["StaticCacheModule_watcher"] = watcher; context.Response.Filter = watcher; }
private void Finish(object sender,EventArgs e) { var watcher = context.Context.Items["StaticCacheModule_watcher"] as StreamWatcher; if (watcher != null) { string value = watcher.ToString().Trim(); } }
public class StreamWatcher : Stream
{
private Stream _base;
private MemoryStream _memoryStream = new MemoryStream();
public StreamWatcher(Stream stream)
{
_base = stream;
}
public override void Flush()
{
_base.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return _base.Read(buffer, offset, count);
}
public override void Write(byte[] buffer, int offset, int count)
{
_memoryStream.Write(buffer, offset, count);
_base.Write(buffer, offset, count);
}
public override string ToString()
{
return Encoding.UTF8.GetString(_memoryStream.ToArray());
}
#region Rest of the overrides
public override bool CanRead
{
get { return _base.CanRead; }
}
public override bool CanSeek
{
get { return _base.CanSeek; }
}
public override bool CanWrite
{
get { return _base.CanWrite; }
}
public override long Seek(long offset, SeekOrigin origin)
{
return _base.Seek(offset, origin);
}
public override void SetLength(long value)
{
_base.SetLength(value);
}
public override long Length
{
get { return _base.Length; }
}
public override long Position
{
get
{
return _base.Position;
}
set
{
_base.Position = value;
}
}
#endregion
}
Topic author was deleted
Need to grab the contents of ClientDependency.Core.Module.ResponseFilterStream
This is a difficult one so apologies ahead of time. The following piece of code works in v4, but not v6 inside an HttpModule.
The main differences I can find between v4 and v6 is the output filtering is the filter type:
In v4 the type is: System.Web.HttpResponseStreamFilterSink
In v6 the type is: ClientDependency.Core.Module.ResponseFilterStream
In v6, 'filter' is null because it fails a cast.
What I'm truly after is the final HTML on every request. I don't actually need to perform my own filtering. Any suggestions on how to read the stream?
The ResponseCaptureStream class is as follows:
I have tried to simply extend the ClientDependency class to no avail.
The underlying type of Filter should always be a Stream, so can you just do...
The constructer for ResponseCaptureStream takes a Stream, so should be able to cope with it.
System.Web.HttpResponseStreamFilterSink and ClientDependency.Core.Module.ResponseFilterStream both derive from Stream.
The approach that I take on a few of my packages that make use of the Response.Filter stream is outlined in this blog post:
http://www.west-wind.com/weblog/posts/2009/Nov/13/Capturing-and-Transforming-ASPNET-Output-with-ResponseFilter
See my code examples for SafeMailLink (here) and Shortcodes (here). Not sure if this approach is 100% applicable to your issue though - sorry.
Cheers, Lee.
Comment author was deleted
A bit more code to chew on:
I do it slightly differently, here's some code. I can't remember why I stick the StreamWatcher in the context items, but there was a good reason (probably haha).
Comment author was deleted
I'm spinning up your code now, thanks for the lead
Comment author was deleted
Brilliant, it seems to be working :) Thank you so much :)
is working on a reply...