Disable Caching in an HttpHandler

by ashic 18. September 2008 05:56

I was generating some custom reports in Word 2007 format today. The reports were being served by an HttpHandler and various params are passed to it (mostly by query string). One report needed a list of ids to be passed and the query string wasn't an option there, so I put that in Session. [My other post today shows how]. The trouble was that the urls were identical and someone clever (the browser or the server) was caching the report. So, changing the parameter that was made up of ids resulted in no change of the report. Now, output caching is pretty simple to eliminate on pages, and for asmx web services for that matter, but I found that doing so for a handler is slightly tricky. Here's what I did:

context.Response.Clear(); context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.Cache.SetExpires(DateTime.MinValue);

 in the ProcessRequest method.

I would've thought context.Response.Cache.SetCacheability(HttpCacheability.None) would do it, but that kept giving me errors when downloading the docx file. Rather, enabling cacheing and forcing a timeout seems to do it.

 

Hope that helps.

Share or Bookmark this post…
  • Facebook
  • DotNetKicks
  • Digg
  • LinkedIn
  • Technorati
  • del.icio.us
  • Google
  • Live
  • Tumblr
  • msdn Social
  • Ping.fm
  • Reddit
  • Slashdot
  • StumbleUpon
  • TwitThis
Categories: .NET | ASP.NET

Comments

8/28/2008 7:11:23 AM #

http://

I had an issue recently with Firefox 3 (FF2 was fine) loading an out of date browser-cached version of a page.

To get FF3 to check and use the new version I had to add 2 more calls on the cache:

      Response.Cache.SetNoStore();
      Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);

I got the info here: www.alittlemadness.com/.../

http://

8/28/2008 3:35:27 PM #

I didn't get any problems with FF3, but I agree. A lot of caching stuff is like hit or miss - somethings appear to work somewhere, but not everywhere. I wish the browsers would be consistent. We always seem to blame IE for this, but FF has its own share of nuances. Every browser out there has its own definition of "standards".

HeartattacK

4/20/2009 8:53:08 AM #

Thanks for Tips, but it didn't solve my problem.

I have perform Image Caching by following instruction of this article www.codeproject.com/.../CachingImagesInASPNET.aspx Based on it, I have one question.

What if I have a portal in which user can change their profile image, and as soon as user change their profile image, new image should display rather than cache image, is it possible that i can remove previously stored user image from cache? One way is to change name of image, but it is not possible because of nature of application, so how can i change image instantly on change of user image? Example: User4.jpg is name of image, this image name will remain same when User upload new image, but new image will never appears until cache expires so he will always see old User4.jpg image despite he just changed the image. Is there any way I can disallow caching of Image for particular folder? or any solution for this problem? Its very hard for me to figure out solution for this problem, your help would very much appreciated.

dotnetguts

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.1.0
Theme by Ashic Mahtab

Need an expert?

Ashic Mahtab
ashic@live.com
(+44) 07879927393

Stats

Featured Ads

 

Donations

I maintain this site and create all content entirely in my own time just to help you guys out. If you find the stuff helpful, or cool or just like what you see, I'd appreciate you chipping in to help out with the hosting costs. It's easy to do so - just click the button below - no amount is too low :)