asp.net-mvc 缓存 CSS 和 JS 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3644748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cache CSS and JS files
提问by Darmak
When I refresh my website in less than 2-3 minutes, Firebug shows these nice requests:
当我在不到 2-3 分钟的时间内刷新我的网站时,Firebug 会显示这些不错的请求:
1. /core.css 304 Not modified
2. /core.js 304 Not modified
3. /background.jpg 304 Not modified
BUT when I refresh after >3 minutes, I get:
但是当我在 >3 分钟后刷新时,我得到:
1. /core.css 200 OK
2. /core.js 200 OK
3. /background.jpg 304 Not modified
Why my CSS and JS files are downloaded again and images aren't?
为什么我的 CSS 和 JS 文件会再次下载而图像却没有?
I'm using ASP.NET MVC 3, I DON'T use [OutputCache]
, and in my /Content
folder (where all css, js and img files live in subfolders) I have this Web.config:
我正在使用 ASP.NET MVC 3,我不使用[OutputCache]
,并且在我的/Content
文件夹中(所有 css、js 和 img 文件都位于子文件夹中)我有这个 Web.config:
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
</staticContent>
</system.webServer>
</configuration>
which sets HTTP header Cache-Control: max-age=86400
ONLY. So basically CSS, JS and images are treated the same way, but somehow CSS and JS don't get cached for a longer period... why is that?
仅设置 HTTP 标头Cache-Control: max-age=86400
。所以基本上 CSS、JS 和图像的处理方式相同,但不知何故 CSS 和 JS 不会被缓存更长的时间......为什么会这样?
采纳答案by Yannis
Hopefully this will help: http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache
希望这会有所帮助:http: //www.iis.net/ConfigReference/system.webServer/staticContent/clientCache
The
<clientCache>
element of the<staticContent>
element specifies cache-related HTTP headers that IIS 7 and later sends to Web clients, which control how Web clients and proxy servers will cache the content that IIS 7 and later returns...
在
<clientCache>
该元素<staticContent>
元素指定IIS 7和更高的缓存相关的HTTP头发送到Web客户端,该如何控制Web客户端和代理服务器将缓存是IIS 7所更高回报的内容?
回答by bodee
This occurs with IIS or with the Visual Studio web server? for some time perceived this behavior while developing (using the VS web server), but when publish it in IIS this not occur anymore.
这发生在 IIS 还是 Visual Studio Web 服务器上?一段时间以来,在开发(使用 VS Web 服务器)时发现了这种行为,但是在 IIS 中发布时,这种情况不再发生。