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.

7
  • Do you have apache? It's been a long time since ive done webdev and not sure if its compatible with asp. Commented May 12, 2012 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. Commented May 12, 2012 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. Commented May 12, 2012 at 14:40
  • Does IIS return a Cache-Control: private response header for the .js and .css files? Commented May 12, 2012 at 15:06
  • @MichaelLiu: How do I check this? Commented May 12, 2012 at 15:40

2 Answers 2

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?<%= ... %>">
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.