asp.net mvc compress stream and remove whitespace
- by Bigfellahull
Hi,
So I am compressing my output stream via an action filter:
var response = filterContext.HttpContext.Response;
response.Filter = new DeflateStream(response.Filter), CompressionMode.Compress);
Which works great. Now, I would also like to remove the excess whitespace present. I found Mads Kristensen's http module http://madskristensen.net/post/A-whitespace-removal-HTTP-module-for-ASPNET-20.aspx.
I added the WhitespaceFilter class and added a new filter like the compression:
var response = filterContext.HttpContext.Response;
response.Filter = new WhitepaperFilter(response.Filter);
This also works great. However, I seem to be having problems combining the two!
I tried:
var response = filterContext.HttpContext.Response;
response.Filter = new DeflateStream(new WhitespaceFilter(response.Filter), CompressionMode.Compress);
However this results in some major issues. The html gets completely messed up and sometimes I get an 330 error.
It seems that the Whitespace filter write method gets called multiple times. The first time the html string is fine, but on subsequent calls its just random characters.
I thought it might be because the stream had been deflated, but isnt the whitespace filter using the untouched stream and then passing the resulting stream to the DeflateStream call?
Any ideas?