views:

146

answers:

3

We are creating a large secure back office web application in ASP.NET. All access to the site is over https connections, and we'd like to either turn off caching for pages or set caches to expire quickly.

However, the site uses quite a few images and largish javascript files/libraries. Is there a way to selectively cache certain files or file types so they are not being reloaded all the time?

A: 

Please look into headers of the sort:

  • Expires: Mon, 23 Nov 2009 07:30:00 GMT
  • Cache-Control: public, max-age=86400

You should read, or skim, Caching in HTTP.

With ASP.NET MVC, you will want to adorn your controller actions with the OutputCacheAttribute. For example:

[OutputCache(Duration = 24*60*60)] // Cached for 1 day
public ActionResult Index() { return View(); }

In ASP.NET you can use the OutputCache directive in your aspx file:

<%@ OutputCache Duration="86400" %>
Frank Krueger
We're doing that for the aspx files. We're attempting to cache images and included javascript files only (but not the aspx files themselves).
Jess
Just set those headers as needed on your webserver then. What are you using? IIS? Apache?
Frank Krueger
IIS ... I think I just found that same answer via Google ... posted answer here, wasn't sure if I was missing something.
Jess
A: 

After some additional Googling, it looks like I might just need to create a web.config in my Scripts and Images folders (and any others I'd like to cache):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
     <staticContent>
        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
     </staticContent>
  </system.webServer>
</configuration>

Does this seem right? Did I miss something?

Jess
This presumably works fine over HTTP, but has no effect over HTTPS does it? Using Fiddler I can see JS files being re-fetched every time the are accessed even with the web.config above in my scripts folder.
MarkD
A: 

You need to use the "far future expire" pattern. Without using output cache, here's how you can do it. (Personally I think this looks nicer than output caching.)

HttpContext.Response.ExpiresAbsolute = DateTime.Now.AddYears(5);

Of course you can change the DateTime to whatever you want.

zowens