5

One solution would be to add a QueryString variable to the url that is a random guid, but that seems a little messy. Is there a setting somewhere that will prevent the browser from displaying a cached version of a page?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Slim
  • 5,635
  • 7
  • 31
  • 30

6 Answers6

9

You can add a meta-tag like this.

<meta http-equiv="pragma" content="no-cache" />
janhartmann
  • 14,713
  • 15
  • 82
  • 138
6

In our ASP.Net projects we create a BasePage all other pages inherit from. In the base page we have a function

Public Sub DisableCaching()
    With Response
        .Expires = 0
        .ExpiresAbsolute = Date.Today.AddDays(-1)
        .AddHeader("pragma", "no-cache")
        .AddHeader("cache-control", "no-cache")
    End With
End Sub

We call that on any page we don't want cached.

Gary.Ray
  • 6,441
  • 1
  • 27
  • 42
  • the lines: 1.- .AddHeader("cache-control", "no-cache") 2.- .CacheControl = "no-cache" Are doing exactly the same thing that is adding a header cache-control with "no-cache" value. – backslash17 May 13 '09 at 04:14
  • Ath what stage of the cycle would be the most effective place to call this method? – Ant Swift Aug 29 '10 at 21:40
  • In our projects it is usually the first line in Page_Load. – Gary.Ray Aug 31 '10 at 14:17
3

ONE aproach would be to add an 'Expires or a Cache-Control Header'.

This has been extracted from Yahoo Best Practices (http://developer.yahoo.com/performance/rules.html)

There are two things in this rule:

* For static components: implement "Never expire" policy by setting far future Expires header
* For dynamic components: use an appropriate Cache-Control header to help the browser with conditional requests

Browsers (and proxies) use a cache to reduce the number and size of HTTP requests, making web pages load faster. A web server uses the Expires header in the HTTP response to tell the client how long a component can be cached. This is a far future Expires header, telling the browser that this response won't be stale until April 15, 2010.

  Expires: Thu, 15 Apr 2010 20:00:00 GMT

If your server is Apache, use the ExpiresDefault directive to set an expiration date relative to the current date. This example of the ExpiresDefault directive sets the Expires date 10 years out from the time of the request.

  ExpiresDefault "access plus 10 years"

So essentially you will be able to set the expiry date to 'inform' the browser that cached component has expired. Therefore the browser will go and request for the site again.

Assuming you need this for web development, another way would be to force clear the cache. on Firefox, this can be done through CTRL + F5 or CTRL + SHIFT + R.

Hope this helps, Lucas

Lycana
  • 1,154
  • 4
  • 10
  • 17
3

Set the Cache-Control header to no-cache.

whaley
  • 16,075
  • 10
  • 57
  • 68
2

Try any of this:

Response.Cache.SetExpires(DateTime.Now.AddSeconds(360));
Response.Cache.SetCacheability(HttpCacheability.Private)
Response.Cache.SetSlidingExpiration(true);

Page.Response.Cache.SetCacheability(HttpCacheability.NoCache)

Also see this question.

Community
  • 1
  • 1
eKek0
  • 23,005
  • 25
  • 91
  • 119
1

One solution would be to add a QueryString variable to the url that is a random guid, but that seems a little messy

Why messy? It's the most reliable way. It is not necessary guid, it could be current time.

Shrike
  • 9,218
  • 7
  • 68
  • 105
  • 1
    I agree. This is a pretty fail-safe way of accomplishing what you want across a wide range of browsers. I typically just add a parameter to the querystring like &nocache={URLENCODEDDateTime} – JohnFx May 12 '09 at 22:45
  • One major disadvantage is search engines may consider every single URL to be a separate page. e.g. index?guid=1234 and index?guid=1235 are distinct URLs as far as most search engines are concerned, even if they contain the exact same content. URLs are for identifying resources, headers are for controlling caching. – Frank Farmer May 20 '09 at 19:16