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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:27:51  来源:igfitidea点击:

Cache CSS and JS files

asp.net-mvccaching

提问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 /Contentfolder (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=86400ONLY. 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 中发布时,这种情况不再发生。

回答by Peter

Could this be the bug in Firefox described here?

这可能是此处描述的 Firefox 中的错误吗?

You could test this by opening the same page in another browser and check what get's loaded using Fiddleror some other tool.

您可以通过在另一个浏览器中打开同一页面来测试这一点,并使用Fiddler或其他一些工具检查加载了哪些内容。