【发布时间】: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?
【问题讨论】:
-
当您调用
Configuration.GetSectiondocs.microsoft.com/en-us/dotnet/api/… 时,您应该提供与 XML 元素匹配的有效位置路径。那么你想要的就很容易实现了。
标签: c# asp.net iis-7 httpmodule