Update: FIX posted here
In a previous post, I wrote about how to render a ViewPage to a string before writing it to the OutputStream. This would be helpful, for instance, to replace certain text with other text (in my case, I used this to replace special tags with other content). The method used basically worked like this:
- Add MemoryStream as a ResponseFilter
- Render output
- Flush the Response so that now the text is in the MemoryStream
- Use a StreamReader to ReadToEnd
- Do the transformations and Write to the Response stream
As it turns out this effectively disabled Page Caching. We tracked it down to step 3 – Response.Flush. To me this call looked innocuous, but that's not how it looks to IIS. I posted the issue to StackOverflow and user Womp referred me to this blog post at IIS.NET.
If some module already flushed the response by the time request reaches UpdateRequestCache stage or if headers are suppressed, response is not cached in output cache module.
That sort of makes sense though, since caching the response requires the entire response to be available.
I have a solution I'm working on, and I plan to write about it soon. In the meantime, don't flush your response if you need page caching.