【问题标题】:Web.Config - StaticContent ClientCache setup for specific filesWeb.Config - 特定文件的静态内容客户端缓存设置
【发布时间】:2019-08-09 07:34:31
【问题描述】:

我希望能够对我网站上的静态文件应用缓存。

我希望缓存仅适用于特定的文件扩展名,但我不能 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 节点不直接绑定到 mimeMap 语句。我不一定希望clientCache 用于指定列表之外的文件。

另外,我应该警惕这种方法有什么“陷阱”吗?

感谢您的帮助。

网站详情:

  • ASP.NET MVC 3
  • IIS7

【问题讨论】:

    标签: c# asp.net asp.net-mvc iis-7 web-config


    【解决方案1】:

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

        <?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>
    

    【讨论】:

    • 是的。据我所知。我最初的问题是确认语法和在使用该设置时我应该注意的任何警告。
    • 如果不想使用,可以使用位置标签或 URL 重写规则。
    • 感谢您的意见。我已经继续使用上面的代码,它似乎运行良好
    • 如果您的问题得到解决,请将帖子标记为答案,这将帮助面临类似问题的其他人。
    猜你喜欢
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 2016-10-25
    • 2016-11-02
    • 1970-01-01
    • 2019-07-12
    • 2011-08-19
    • 1970-01-01
    相关资源
    最近更新 更多