How to read the complete html in Response.Filter.Write method?

I am using a Response.Filter. I am overriding the Write method. But I am observing that the Write method is executing many times and the response html in Write method come into chunked. How can I read(and then filter) the complete html in Write method.

Upvotes: 0

Views: 310

Answers (1)

Tim Rogers
Tim Rogers

Reputation: 21713

You should avoid doing this as it may have a performance impact on your website. Filters are streams, they are low-level, and meant for working directly with the response bytes, so are ideal for doing things like Gzip compression.

You should try and do your filter operations in chunks ideally. If you really can't, you can write a filter based on a MemoryStream. Remember you will be creating an in-memory buffer so be sure that your responses will be relatively short.

public class ReadAllIntoMemoryFilter : MemoryStream
{
    private readonly Stream _baseFilter;

    public ReadAllIntoMemoryFilter(Stream baseFilter)
    {
        _baseFilter = baseFilter;
    }

    public override void Close()
    {
        var bytes = GetBuffer();

        // do your work here

        _baseFilter.Write(bytes, 0, bytes.Length);
        _baseFilter.Close();
        base.Close();
    }
}

Upvotes: 1

Related Questions