How to specify an HTTP expiration header? (ASP.NET MVC + IIS)

I already use output caching in my ASP.NET MVC application.

Page speed tells me to indicate the expiration of the HTTP cache for css and the images in the response header.

I know that the Response object contains some properties that control the expiration of the cache. I know that these properties can be used to control HTTP caching for the response that I serve from my code:

Response.Expires Response.ExpiresAbsolute Response.CacheControl 

or alternatively

 Response.AddHeader("Expires", "Thu, 01 Dec 1994 16:00:00 GMT"); 

Question: how to set the Expires header for resources that are serviced automatically, for example. images, css, etc.

+46
caching asp.net-mvc iis
Apr 09 2018-10-09T00:
source share
3 answers

Found:

I need to specify a client cache for static content (in web.config).

 <configuration> <system.webServer> <staticContent> <clientCache cacheControlCustom="public" cacheControlMaxAge="12:00:00" cacheControlMode="UseMaxAge" /> </staticContent> </system.webServer> </configuration> 

from http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache

+74
Apr 09 '10 at 15:50
source

If you want to do this from the code for the returned resource (i.e. not a static file served from IIS), you'd better use Response.Cache :

 Response.Cache.SetExpires(DateTime.Now.AddYears(1)); Response.Cache.SetCacheability(HttpCacheability.Public); 

I know that it’s not quite what you are asking for, but I found this question through Google and thought that others might like this answer because it is related to the APIs that you show in the source text of the question.

+25
Jun 22 2018-11-11T00:
source
+3
Apr 09 '10 at 16:28
source



All Articles