Tutorial :Browser Caching in ASP.NET application


Any suggestions on how to do browser caching within a asp.net application. I've found some different methods online but wasn't sure what would be the best. Specifically, I would like to cache my CSS and JS files. They do change, however, it is usually once a month at the most.


Another technique is to stores you static images, css and js on another server (such as a CDN) which has the Expires header set properly. The advantage of this is two-fold:

  1. The expires header will encourage browsers and proxies to cache these static files
  2. The CDN will offload from your server serving up static files.
  3. By using another domain name for your static content, browsers will download faster. This is because serving resources from four or five different hostnames increases parallelization of downloads.
  4. If the CDN is configured properly and uses cookieless domain then you don't have unnecessary cookies going back and forth.


Its worth bearing in mind that even without Cache-Control or Expires headers most browsers will cache content such as JS and CSS. What should happen though is the browser should request the resource every time its needed but will typically get a "304 Unmodified" response and the browser then uses the cached item. This is can still be quite costly since its a round trip to the server but the resource itself isn't sent so the bytes transfered is limited.

IE left with no specific instructions regarding caching will by default use its own heuristics to determine if it should even bother to re-request an item its cached. This despite not being explicitly told that it can cache a resource. Its hueristics are based on the Last-Modified date of the resource, the older its the less likely it'll have changed now is its typical reasoning. Very wooly.

Frankly if you want to make a site perfomant you need to have control over such cache settings. If you don't have access to these settings then don't wouldn't worry about performance. Just inform the sponsor that it may not perform well because they haven't facilitated a platform that lets you deliver that.


You best bet to do this is to set an Expires header in IIS on the folders you want the content cached. This will tell most modern browsers and proxies to cache this static content. In IIS 6:

  1. Right click on the folder (example CSS or JS) you want to be cached by the browser.
  2. Click properties
  3. Go to the HTTP Headers Tab
  4. Check "Enabled content expiration"
  5. Set some long period for expiration, like "Expires after 90 days"

Yahoo Developer's Blog talks about this technique.


Unless you configure IIS to give asp.net control of js/css/image requests it won't see them by default, hence your best plan (for long term maintainability) is to deliberately tweak the response headers at your firewall/trafficmanager/server or (better and what most of the world does at this point) to version your files in path, i.e:

Instead of writing this in your mark-up:


Use this:


..and change the version number when you need to effectively clear cache. If that's every time then you could even append a GUID or datestamp instead, but I can't think of any occasion where I would want to do that really.

I thought your question was anti-cache but re-reading it I see I wasted a good answer there :P

Long story short, browsers are normally very aggressively pro-caching "simple" resources so you shouldn't have to worry about this, but if you did want to do something about it you would have to have access to a firewall/trafficmanager/IIS for the reasons above (ASP.NET won't be given a chance by default).

However... there is no way you can absolutely force caching, and nor should you. What is and isn't cached is rightfully the decision of the end-user, all you can do is strongly request.


In .net you can set up your JavaScript, CSS and Images as embedded resources. .Net will then handle the file expiration for you. The downside to this approach is that you have to do a new build for each set of changes (this might be an upside, depending on your deployment and workflow).

You could also use ETags, but from what I understand in some cases it doesn’t work well if you have mix of IIS and apache Webservers hosting your images, (or if you plan to switch in the future).

You can just make sure the file date is newer, and let the server handle it for you, but you’ve got to make sure the server is configured correctly.


You can cache static content by adding following code in web.config

 <system.webServer>      <staticContent>        <clientCache httpExpires="Tue, 12 Apr 2016 00:00:00 GMT" cacheControlMode="UseExpires" />      </staticContent>  </system.webServer>  

See the clientCache documentation for more details.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »