c# - Web.Config - 特定文件的 StaticContent ClientCache 设置

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

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

我希望缓存仅应用于特定的文件扩展名,但我不能 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/

相关文章:

asp.net-mvc - 排除一些子成员获得验证

c# - 在 Visual Studio 中调试 C# 代码时,是否可以轻松导出 xml、csv 或文本格式的列表或字典?

c# - “System.Reflection.TargetInitationException”(MVVM Light 内部)

c# - 执行自定义查询—— Entity Framework

asp.net - 如何在 ASP.NET 表单例份验证中更改 "ReturnUrl"查询字符串键的名称?

ASP.Net Web 应用程序忽略 global.asax 文件和代码隐藏更改

C#MVC : Chrome using the action name to set inline PDF title

c# - 将控制权传递给 ASP.NET 中的父类/基类的最佳方式

c# - 将 DateTime 存储到 cookie 中的最佳方法是什么?

jquery - 使用 jquery 触发链接的最佳方法