Thursday, November 9, 2006

Caching in .NET handlers

Browser caching of a .NET handler may prevent the server-side code of the handler to be invoked. A user requesting an action for a second time may not get his/her results because your handler is cached and the invocation does not occur.

This problem also depends on how long the handler is being cached. If in your browser you delete all offline content (Tools - Internet Options - Delete Fiels - Check Delete all offline content - OK - OK) then your handler is no longer cached.


The solution is to add the following code to the ProcessRequest function of the handler:

// Don't allow this response to be cached by the browser.
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetNoStore();
context.Response.Cache.SetExpires(DateTime.MinValue);




Isaac Levy, August 2006

No comments: