1

In my master page in the Page_Load method, I have this line:

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Basically, I don't want to cache the page. I do want to cache the .js and .css files but when I reload the page, these files don't get loaded from the browser cache but instead get reloaded.

What do I need to fix?

Thanks.

frenchie
  • 51,731
  • 109
  • 304
  • 510
  • Do you have apache? It's been a long time since ive done webdev and not sure if its compatible with asp. –  May 12 '12 at 14:09
  • I don't think js and css files will be affected by the codes in your master page because those files are static contents, rather than asp.net dynamic pages. –  May 12 '12 at 14:11
  • @allentranks: well when I look at the Chrome Network tab, I see that the only files that's reloaded from cache is jquery (it's loading from the google CDN). All others are all reloaded from my server. – frenchie May 12 '12 at 14:40
  • Does IIS return a `Cache-Control: private` response header for the .js and .css files? – Michael Liu May 12 '12 at 15:06
  • @MichaelLiu: How do I check this? – frenchie May 12 '12 at 15:40
  • Under Chrome's Network tab, select the file you want to check, click the Headers tab in the right pane, and then scroll down to Response Headers. Update your question and include the response headers. (Right-click the file and choose "Copy response headers".) – Michael Liu May 12 '12 at 16:02
  • I find Fiddler useful for checking the raw request/response - http://www.fiddler2.com/fiddler2/ – James Manning May 13 '12 at 05:57

2 Answers2

3

Add these lines in the html meta section

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="EXPIRES" CONTENT="0">
<META HTTP-EQUIV="PRAGMA" content="NO-CACHE">

in ASP page

// Stop Caching in IE
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

// Stop Caching in Firefox
Response.Cache.SetNoStore();

This article can be usefull to resolve your issue

http://www.codeproject.com/Articles/11225/Disabling-browser-s-back-functionality-on-sign-out

P.S. If it didn't help. You can use cheat like this: <%= here you should add some random number for example Ticks %> - add it for elements which shouldn't be cached. For example:

<link rel=stylesheet href="main.css?<%= ... %>">
Eugene Trofimenko
  • 1,611
  • 1
  • 13
  • 19
2

You need to set up caching for static resources (.js and .css) files. This is not enabled by default.

See this SO answer for more details: https://stackoverflow.com/a/2196848/69868

Also note that when you are reloading the page using F5/Ctrl+F5, some browsers will ignore caching instructions and they will always fetch fresh content (for example Firefox and Ctrl+F5). If you want to test caching, reload the page by re-entering the URL in location text box.

Community
  • 1
  • 1
Miroslav Bajtoš
  • 10,667
  • 1
  • 41
  • 99