HeartattacK

Disable Caching in an HttpHandler

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.

Posted: Sep 18 2008, 05:56 PM by HeartattacK | with 4 comment(s) |
Filed under: ,

Comments

funny wallpaper » Disable Caching in an HttpHandler said:

Pingback from  funny wallpaper » Disable Caching in an HttpHandler

# September 18, 2008 7:16 AM

AndrewSeven said:

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/.../ajax-vs-caching-vs-firefox-3

# September 18, 2008 9:11 AM

HeartattacK said:

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".

# September 18, 2008 12:35 PM

dotnetguts said:

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.

# May 11, 2009 12:53 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)