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.

<system.webServer>
  <staticContent>
    <clientCache cacheControlMaxAge="14.00:00:00" cacheControlMode="UseMaxAge"/>
  </staticContent>
</system.webServer>

This implementation is confirmed with Firebug as shown in the snapshot taken below.

 

 

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.

About these ads

Tagged: , , , ,

21 thoughts on “How to add expires headers in ASP.NET

  1. jeeshenlee August 10, 2010 at 10:26 pm

    If you are looking for ways to improve your application performance, take a look at another blog post “How to GZip on ASP.NET and GoDaddy”. I manage to compress the data send to the visitor by 50% hence improving the speed by 50%. :)

    http://jeeshenlee.wordpress.com/2010/08/01/how-to-gzip-on-asp-net-and-godaddy/

  2. alaa9jo September 26, 2010 at 9:36 pm

    Unfortunatly It didn’t work for me,I’ve checked with YSlow (add-on for FireFox).

  3. jeeshenlee September 26, 2010 at 9:47 pm

    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

  4. alaa9jo September 26, 2010 at 10:01 pm

    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

  5. jeeshenlee September 26, 2010 at 10:08 pm

    Hello alaa9jo, in case you found the solution for II6, please update us or send me a link to your blog. Thanks!

  6. 2010 in review by Wordpress.com « myNotes January 2, 2011 at 7:27 pm

    [...] How to add expires headers in ASP.NET July 2010 5 comments and 1 Like on WordPress.com, 5 [...]

  7. Claire January 19, 2011 at 9:55 am

    Thanks for the useful tip Jeeshen :-)

  8. jeeshenlee January 19, 2011 at 3:05 pm

    @Clair, Sure. ;)

  9. Rod March 7, 2011 at 3:15 pm

    I need a solution for share hosting on IIS 6. Help! I’ve looked everywhere and this is day 4 of my search .. HELP!!!

  10. Soan April 3, 2011 at 3:35 am

    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.

  11. jeeshenlee April 5, 2011 at 9:19 am

    Hello Soan, hope my tricks here helps you. ;)

  12. Soan April 15, 2011 at 12:51 am

    Ya…i think it works..i just implemented and checked the results.
    Thanks Jeeshenlee.

  13. Eduard July 23, 2011 at 12:34 am

    Nice article, work 4 me.

  14. aplikacje strony internetowe September 8, 2011 at 7:27 pm

    Great article. Finally I found good answer. Everything works like a charm. Thank You!!

  15. [...] un tutorial para crear expires headers en Apache. En este otro artículo verás como crearlo en ASP.NET.3. Comprime los componentes con GzipAl loro con esta regla: no comprimas las imágenes ni los [...]

  16. […] How to add expires headers in ASP.NET […]

  17. kamal October 28, 2013 at 1:21 pm

    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 ??

  18. wordpress Recommended hosting December 10, 2013 at 2:32 am

    Somebody necessarily help to make seriously articles I would state.
    This is the first time I frequented your website page and so
    far? I amazed with the analysis you made to make this particular put up extraordinary.

    Wonderful task!

  19. Blacklist Check December 10, 2013 at 3:31 am

    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.

  20. 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.

  21. Pearl April 6, 2014 at 2:11 pm

    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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 110 other followers

%d bloggers like this: