Thursday, April 19, 2007

ASP.NET Page Caching (aka Page Jumping)

I was recently having an issue with page jumping. Page jumping? Ya, that was my question as well and unfortunately the way it was being described to me by my end users. It seemed that when a customer service agent would view a customer the next agent who performed a search was "jumped" to the previous agents customer rather than the intended one.

It turns out that it had nothing to do with any kind of page jumping at all. The fact that it was being described to me in that way is what threw me off. After further investigation I determined page caching to be the culprit.

Because there were so many agents using the system, if another agent did a search for a customer and loaded the UI within a minute from the last search they were presented with a cached page of the last agents search. Walla! Instant page jumping (I described it as an undocumented feature, HA!).

.Net provides many different ways to control caching. I had used the @Output Cache Duration="1" VaryByParam="none" in my page declarations thinking that I was doing the right thing. However, (I wrote the code a long time ago) just the VaryByParam="none" alone was wrong. I tried setting the vary by to star (*) but I continued to have issues. I could have also turned off caching completely at the sacrifice of performance but this was not what I wanted. So, after a little searching (thanks Google) I found a simple solution.

In my Page_Load event I included the following little snippet:

Response.Cache.SetCacheability(HttpCacheability.NoCache)

The "page jumping" is an issue no more with the ability to turn off caching at an individual page level rather than an entire app.

No comments: