首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Web.Config -针对特定文件的StaticContent ClientCache设置

Web.Config -针对特定文件的StaticContent ClientCache设置
EN

Stack Overflow用户
提问于 2019-08-09 07:34:31
回答 1查看 2.5K关注 0票数 3

我想要能够应用缓存到我的网站上的静态文件。

我希望缓存只适用于特定的文件扩展名,但我不能百分之百地肯定要添加到我的web.config文件中的语法。

到目前为止,这就是我所拥有的:

代码语言:javascript
复制
<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>

我认为这将适用于具有以下扩展名的静态文件,这是正确的吗?

  • .svg
  • .jpg
  • .png
  • .gif
  • .css
  • .js

看起来,配置中的clientCache节点并不直接绑定到mimeMap语句。我不一定希望clientCache处理指定列表之外的文件。

此外,这种方法有什么‘棘手’,我应该警惕吗?

谢谢你的帮助。

网站详情:

  • ASP.NET MVC 3
  • IIS7
EN

回答 1

Stack Overflow用户

发布于 2019-08-12 02:59:31

可以使用以下代码应用客户端缓存控制设置:

代码语言:javascript
复制
    <?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>

注意:使用您的文件扩展名。

您也可以使用location标记来完成此操作,但为此,您需要将特定的文件扩展名文件移动到另一个文件夹--将此设置应用于该文件夹。

代码语言:javascript
复制
  <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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57425432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档