如何将<clientCache />设置应用于IIS中的特定扩展?

Ala*_*lan 10 iis web-config azure azure-web-sites

我在Azure上使用以下Web.config托管Web应用程序:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <staticContent>
      <mimeMap fileExtension=".text" mimeType="text/plain" />
      <clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
    </staticContent>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

这有效,但我想通过扩展来改变缓存时间.我想将.html文件的最大年龄设置为1天,将其他所有文件的最大年龄设置为365天.原因是html中的所有资产都有其文件名在更改时加速并且是从CDN提供的,因此它们永远不需要过期,但是html页面本身需要始终保持新鲜,以便用户可以看到新内容.

我看到该<location>元素允许将Web.config过滤到特定位置,但我没有看到将其限制为某些扩展的方法.

请注意,我不要求能够做到这一点在Web.config:任何可能的手段与Azure的Web应用程序的罚款.

Kul*_*gin 14

正如其他人所说,这是不可能的,所以我想建议一个解决方法来完成这项工作.
您可以通过创建出站规则来替换Cache-Controlhtml文件的标头来利用URL重写模块的功能.

这是配置.

<?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>
Run Code Online (Sandbox Code Playgroud)

我测试的截图:

在此输入图像描述