IIS 8.5 MVC5 Client Cache is ignored

2019-04-11 12:53发布

TL;DR - I want the server (IIS 8.5) to return 304 not modified for the CSS and JS bundles.

I've been unable to get IIS 8.5 to honor the clientCache settings in web.config. No matter what I do, I can't seem to get it to cache the static content. This is a MVC5 app in VS2013. I've got all the static files in a folder "Assets".

The request looks like: http://someserver/AppName/Assets/mainjs?v=FNj_9ZPAbYVAQsyDo2F8XUnWv5NQpY4iX2RGu4NpJ5g1

  1. Attempt #1, place a new web.config in the Assets folder with the following:
<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
    </staticContent>
  </system.webServer>
</configuration>
  1. Attempt #2: place this following configuration in the root web.config
<staticContent>   
    <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />    
</staticContent>
  1. Attempt #3: trying setting the cache using the location tag
<location path="Assets">
  <system.webServer>
    <staticContent>
      <clientCache  cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
    </staticContent>
  </system.webServer>
</location>

Here are the things I've tried in IIS 8.5 manager. Under the Default Web Site/TestApp

  1. HTTP Requeset Headers, Set Common HTTP Headers, check "Expire Web Content" "After 365 Day(s)".
  2. Apply the same setting to the "Assets" folder

I've tried these steps each on their own and all together and every other which way. No matter what, it won't add the max-age value to Cache-Control.

For each of these, the browswer returns 200 responses for the CSS and JS bundles. I cannot get the server cache the content coming from the Assets folder.

Cache-Control:private
Content-Encoding:gzip
Content-Length:22591
Content-Type:text/css; charset=utf-8
Date:Mon, 02 Feb 2015 21:49:49 GMT
Expires:Tue, 02 Feb 2016 21:49:49 GMT
Last-Modified:Mon, 02 Feb 2015 21:49:49 GMT
Persistent-Auth:true
Server:Microsoft-IIS/8.5
Vary:Accept-Encoding
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET

1条回答
做自己的国王
2楼-- · 2019-04-11 13:44

Check the individual files of the bundle. What do the files look like when you turn off optimizations? After you register your Bundles make a call to :

BundleTable.EnableOptimizations = (!HttpContext.Current.IsDebuggingEnabled);

OR

BundleTable.EnableOptimizations = false;

It could be that each file is coming back with a not modified. Does the status change when you refresh while in a debug session?

查看更多
登录 后发表回答