MVC HTTP Caching - The header with the last modified response is always equal to the date

I donโ€™t quite understand how .NET MVC HTTP caching works, because it doesnโ€™t seem like it really extracts cached resource files. I think I need to add some additional code somewhere ...

First, let's see how I set up HTTP caching on static content (i.e. images). In my web.config, I have the following:

<system.webServer> <staticContent> <clientCache cacheControlMode="UseExpires" httpExpires="Tue, 19 Jan 2038 03:14:07 GMT" /> </staticContent> </system.webServer> 

This causes the images in my application to look like caching properly. When I look at the response headers for the image, I see this (deleted unnecessary headers):

  Date:Thu, 27 Feb 2014 16:27:48 GMT ETag:"086f8d199a4ce1:0" Expires:Tue, 19 Jan 2038 03:14:07 GMT Last-Modified:Thu, 29 Aug 2013 09:26:20 GMT 

I see the ETag value, which is good, and my Expires is what it should be. In addition, the date of the last modification in the past. I understand that the Date of Last Modification was the date of the last request for this file.

Now consider the response headers for a javascript file that has been optimized by MVC. As a reminder, this article states that "Bundles set the HTTP Expires header one year after the package was created."

  Cache-Control:public Date:Thu, 27 Feb 2014 16:44:16 GMT Expires:Fri, 27 Feb 2015 16:44:16 GMT Last-Modified:Thu, 27 Feb 2014 16:44:16 GMT Vary:User-Agent 

The response headers for the cached MVC file do not have an ETag for one. There is a Cache-Control value of "public" that was not in the response header of static content. Finally, it expires one year after the date of the last modification, which is correct, but the date of the last modification always matches the date of the date. These response headers seem to me what they would be when the resource is requested from the server for the first time and cached, and not when it is subsequently requested and retrieved from the cache.

Thanks in advance for your understanding.

UPDATE . Actually, this is similar to caching in IE. The date of the last modification of subsequent requests remains valuable in the past. However, I do not see this in FF or Chrome. I confirmed that in both of these browsers I did not disable caching. What gives?

+7
asp.net-mvc expires-header
source share
1 answer

First of all, system.webServer/staticContent/clientCache intended only for static resources (files). Thus, it works if you directly access images.

A package is a dynamic resource (a handler generates content). This is why the configuration directive does not apply.


Secondly, using ETag , Expires , Last-Modified are three different caching methods. You should not use both methods together, as they work differently.

Expires tells the browser to save the file in the cache until the specified date. The browser will never call the server until this date.

ETag is a dynamic caching mechanism. The browser will always call the server , but the server may not respond with content if it has not changed.

Last-Modified is an ancient dynamic caching mechanism. It works the same as ETag , but has the problematic requirement of exposing the correct date of change. What is not easy to find when creating dynamic content.

I think that combining several methods should be reserved only for well-designed cases. See more details.

You can read the basics: HTTP Caching


And then for your problem, I'm not sure why so many headers are displayed by your application. The presence of Last-Modified on the bundle is strange to explain. A package is a virtual thing that provides various files combined as one. Thus, it does not have a real date of the last modification. Do you use additional code / caching modules?

0
source

All Articles