【问题标题】:Adding client cache via httpmodule for custom folder通过 httpmodule 为自定义文件夹添加客户端缓存
【发布时间】:2020-03-13 16:25:02
【问题描述】:

我目前在我的项目的 web.config 中使用。

<location path="js">
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="8765:00:00"/>
    </staticContent>
   </system.webServer>
</location>
<location path="css">
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="8765:00:00"/>
    </staticContent>
   </system.webServer>
</location>

如果我只想在这里和那里缓存几个文件夹,但我在许多其他地方有 javascript 和 css 我也想缓存并选择使用 HttpModule 将这些目录缓存在那里,这会很好。

我做了一些研究,在 clientCache 上遇到了this article from Microsoft,它向我展示了如何使用 c# 在 httpModule 中更新它

  using(ServerManager serverManager = new ServerManager())
  { 
     Configuration config = serverManager.GetWebConfiguration("Default Web Site");
     ConfigurationSection staticContentSection = config.GetSection("system.webServer/staticContent");

     ConfigurationElement clientCacheElement = staticContentSection.GetChildElement("clientCache");
     clientCacheElement["cacheControlMode"] = @"UseMaxAge";
     clientCacheElement["cacheControlMaxAge"] = @"8765:00:00";

     serverManager.CommitChanges();
  }

我的问题是,是否可以使用此方法指定要缓存的确切文件夹?例如,如果我在以下目录中有一个带有 javascript 的文件夹:siteMap/js 如何告诉我的 httpModule 为该显式文件夹设置 clientCache?

【问题讨论】:

标签: c# asp.net iis-7 httpmodule


【解决方案1】:

例如。如果您想通过在 web.config 中添加 &lt;location="Scripts"&gt; 部分来为文件夹“脚本”设置客户端缓存。

那么你可以试试这个 config.GetSection("system.webServer/staticContent", "Scripts"); .

  using(ServerManager serverManager = new ServerManager()) { 
            Configuration config = serverManager.GetWebConfiguration("Default Web Site");

            ConfigurationSection staticContentSection = config.GetSection("system.webServer/staticContent", "Scripts");

            ConfigurationElement clientCacheElement = staticContentSection.GetChildElement("clientCache");
            clientCacheElement["cacheControlMode"] = @"UseMaxAge";
            clientCacheElement["cacheControlMaxAge"] = TimeSpan.Parse("8765.00:00:00");

            serverManager.CommitChanges();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多