Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS7: set "no-cache" for all aspx pages but not images/css/js

I would like to not cache my aspx pages anywhere. For some reason IE ignores meta tags which are set from my master page

<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">

I am trying to see if I can set my Http response header to "Cache-Control" - "no-cache". Setting something like

HttpContext.Current.Response.Headers.Add("Cache-Control", "no-cache");
   HttpContext.Current.Response.Headers.Add("Exipres", DateTime.Now.AddDays(-1).ToShortDateString());

in every page would be painful. I am thinking if there is anyway we can set this in IIS7 (add this header to aspx pages, but not images/css/js). Is it possible ?

Edit: As per suggestion in http://technet.microsoft.com/en-us/library/cc753133%28WS.10%29.aspx, adding a custom http response header adds the header to all files including js,css,images. So adding "Cache-Control","no-cache" here did not work either

Edit2: I am thinking about adding a httpmodule . Something similar to http://blogs.technet.com/stefan_gossner/archive/2008/03/12/iis-7-how-to-send-a-custom-server-http-header.aspx. Any suggestions ?

like image 338
ram Avatar asked Jan 07 '10 19:01

ram


3 Answers

http://technet.microsoft.com/en-us/library/cc770661(WS.10).aspx

By default IIS only caches static content; you'll have to make adjustments if it's caching non-static content already.

like image 186
jvenema Avatar answered Oct 23 '22 05:10

jvenema


If you are using a MasterPage for your site, you may want to consider adding the following response header to its Page_Load event:

protected void Page_Load(object sender, EventArgs e)
{
Response.AddHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
}

Since your .js file(s) will not use the MasterPage, the browser should save the reference to its cache.

The example above is what I use and it works well under Firefox3, IE7, and Chrome7. Note that the response header above is the only thing I added for cache control and it does the job. However, I often see Pragma and Expires response headers on other websites.

For example, here is the response headers that are used in Gmail:

Content-Type: text/html; charset=UTF-8
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT

Date: Tue, 02 Nov 2010 16:38:15 GMT
x-dns-prefetch-control: off
Content-Encoding: gzip
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSEServer: GSE

I'm not sure if those are used for older browsers and/or other newer browsers.

I prefer to implement the minimum amount of code to solve a problem and I've never (yet) had a case where the Response.AddHeader noted at the top wasn't sufficient.

like image 32
Jed Avatar answered Oct 23 '22 04:10

Jed


One absolutely definite way to keep any browser from caching your page would be to add a query string variable set to a random number, so your links would always end in "?ran=". I've done that on a limited basis in the past.

like image 42
Yoenhofen Avatar answered Oct 23 '22 03:10

Yoenhofen