How to Add Expires Headers on an ASP.NET MVC Website

How to Add Expires Headers on an ASP.NET MVC Website

  A web server uses the Expires header in the HTTP response to tell the client how long a component can be cached. Browsers use a cache to reduce the number and size of HTTP requests, making web pages load faster. 

YSlow to give me an A on the "Add Expires header" section by setting the web.config file.

So, if you want to use the Http expires headers for your static content, set it like this:-

Add this to your system.webserver web.config file

<staticContent>
  <clientCache cacheControlMode="UseExpires" httpExpires="Thu, 17 Sep 2020 00:00:00 UTC" />
</staticContent>

 

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

 

In the above example Expires are set for 10 days, 0 hours, 0 minutes, and 0 seconds.

 

Response Header
Cache-Control: private, max-age=259200
Content-Encoding: deflate
Content-Length: 3074
Content-Type: text/html; charset=utf-8
Date: Thu, 17 Sep 2020 03:17:43 GMT
ETag: 277f579d-5244-40b1-8
Expires: Sun, 20 Sep 2020 03:17:43 GMT
Last-Modified: Thu, 17 Sep 2020 03:17:43 GMT
SERVER
Vary: Accept-Encoding
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Cookie: _ga=GA1.2.255980350.1578940101; __gads=ID=d5c03ac2c33deef2-228f8ef4d8c20009:T=1597040533:RT=1597040533:S=ALNI_MYpWx_Bi2Nx4ULiXB2GZ45XD7B7HA; _gid=GA1.2.914256716.1600058412; ASP.NET_SessionId=a1cch5lxtihysvtclas3hakt; _gat_gtag_UA_153406186_1=1
Host: www.minifycode.in
If-Modified-Since: Wed, 16 Sep 2020 12:17:53 GMT
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36

2021-10-03