How to add expires headers in ASP.NET

Referring to the article “Best Practices for Speeding Up Your Web Site” from YUI, adding “Expires Header” to static files can improve the load time of the web site. After searching around for answer on how to add expires header to my static files (e.g JPG, CSS, JS and etc), I found the solution below is the easiest to implement.

Add the configuration shown below in the in web.config file. Below is the sample configuration setup that cache the static files for 14 days before expired.

Add Expires header in ASP.NET using <clientCache> in web.config.

I don’t get paid for writing blog posts. Your “Like” is my only motivation, so please “Like” it if you found this information useful. Cheers!!!

Check out what other says about this post by clicking at the title of this post.

24 comments

    1. Hello alaa9jo, I’m sorry that it didn’t work out for you. May I know where you host your app? and did you hit “Ctrl+F5″ to confirm (just in case you don’t know)?

      It happen to me once that the static header configuration took effect only few hours later after I made the changes. Try check again. Let me know, I would like to help.

      Regards,
      Jeeshen Lee

  1. My mistake,I’ve did a search over the internet and I discovered that this method works only for IIS 7 :(,that’s why it didn’t work for me,I have IIS 6 installed.

    P.S: Now I noticed that this article has a tag: IIS 7 :P

  2. Finally found what i was looking for. It seems nobody has asked this on Godaddy support as i could not see a single link to Godaddy community in Google results.

    Will try changing my web.config and update the results here. Also, it is good for me that the solution is for IIS7. I have just upgraded to Godaddy 4G hosting and it uses IIS7.

  3. hi JeeShen Lee,
    i am applying your trick in web.config file and for deployment i am using IIS 7.0

    the cache should be expire after 1 day
    but it seems not working for me my response header is not showing updated expire time.

    my respone is

    Data Size 827
    Device disk
    Expires Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)
    Fetch Count 17
    Last Fetched Mon Oct 28 2013 10:50:41 GMT+0530 (India Standard Time)
    Last Modified Mon Oct 28 2013 10:50:38 GMT+0530 (India Standard Time)

    Note: i want to add one more ques that browser cache images/js/css automatically or we need to cache it programatically ??

  4. Gotta say, I do love MXToolbox. I found it while working for a marketing agency with 200 IP addresses for email campaign sending, and using the blacklist check lookup and service helped me to stay ahead of the blacklists. Well done, MX! My next challenge is to figure out better ways to report spam from other companies that don’t adhere to the same quality control that ours did.

  5. Imagine the freedom of being able to work from your
    own home and tell me honestly that you have never
    considered the benefits. And, by diversifying your method of generating
    income, you can even create as many “mountains” as you like.
    Writing copy for brochures, leaflets and newsletters – This type of copywriting harks back to
    more traditional copywriting, and this opportunity can be
    pursued both on and offline.

  6. Greetings! I’ve been following your web site for some time now and finally got the courage to go ahead and give you a shout out from Porter Tx!

    Just wanted to tell you keep up the excellent job!

  7. Hi Jeeshen, FYI below is the response header.

    Cache-Control private, max-age=0
    Connection keep-alive
    Content-Length 26049
    Content-Type application/json; charset=utf-8
    Date Tue, 06 May 2014 18:23:51 GMT
    Server Microsoft-IIS/8.0
    X-AspNet-Version 4.0.30319
    X-Powered-By UrlRewriter.NET 2.0.0, ASP.NET