我希望能够将缓存应用于我网站上的静态文件。
我希望缓存仅应用于特定的文件扩展名,但我不能 100% 确定要添加到我的 web.config
文件中的语法。
这是我目前所拥有的:
<staticContent>
<remove fileExtension=".svg" />
<remove fileExtension=".jpg" />
<remove fileExtension=".png" />
<remove fileExtension=".gif" />
<remove fileExtension=".css" />
<remove fileExtension=".js" />
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
<mimeMap fileExtension=".svg" mimeType="image/svg+xml"/>
<mimeMap fileExtension=".jpg" mimeType="image/jpg"/>
<mimeMap fileExtension=".png" mimeType="image/png"/>
<mimeMap fileExtension=".gif" mimeType="image/gif"/>
<mimeMap fileExtension=".css" mimeType="text/css"/>
<mimeMap fileExtension=".js" mimeType="text/javascript"/>
</staticContent>
我认为这将对具有以下扩展名的静态文件应用 1 天缓存是否正确?
- .svg
- .jpg
- .png
- .gif
- .css
- .js
它看起来配置中的clientCache
节点没有直接绑定(bind)到mimeMap
语句。我不一定希望 clientCache
为指定列表之外的文件工作。
此外,我应该警惕这种方法是否存在任何“陷阱”?
感谢您的帮助。
站点详细信息:
- ASP.NET MVC 3
- IIS7
最佳答案
您可以使用以下代码应用客户端缓存控制设置:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".text" mimeType="text/plain" />
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
<rewrite>
<outboundRules>
<rule name="RewriteCacheControlForHTMLFiles" preCondition="FileEndsWithHtml">
<match serverVariable="RESPONSE_Cache_Control" pattern=".*" />
<action type="Rewrite" value="max-age=86400" />
</rule>
<preConditions>
<preCondition name="FileEndsWithHtml">
<add input="{REQUEST_FILENAME}" pattern="\.html$" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
注意:使用您的文件扩展名。
您也可以使用位置标签来执行此操作,但为此,您需要将该特定文件扩展名文件移动到另一个文件夹,并将此设置应用于该文件夹。
<configuration>
<!-- Note the use of the 'location' tag to specify which
folder this applies to-->
<location path="images">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:00:15" />
</staticContent>
</system.webServer>
</location>
</configuration>
关于c# - Web.Config - 特定文件的 StaticContent ClientCache 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57425432/