假设您使用的是 IIS 或 IIS Express,最简单的解决方案就是通过向web.config
文件添加设置来完全禁用开发环境中的缓存。
注意:如果您没有web.config
文件,您可以在网站的根目录中创建一个。
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="no-cache" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
或者,您可以禁用网站特定位置的文件缓存:
<configuration>
<location path="path/to/the/file">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
</location>
</configuration>
无论哪种情况,您都可以在发布期间使用Web 配置转换,以便在测试/生产环境中删除这些部分。
<?xml version="1.0" encoding="utf-8"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!-- Use this section if you are disabling caching site-wide -->
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="no-cache" xdt:Transform="Remove" xdt:Locator="Match(name)" />
</customHeaders>
</httpProtocol>
</system.webServer>
<!-- Use this section if you are disabling per folder (duplicate if necessary) -->
<location path="path/to/the/file" xdt:Transform="Remove" xdt:Locator="Match(path)">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
</location>
</configuration>
参考:如何使用 weserver 配置设置禁用 IIS 7 中单个文件的缓存