0

我正在使用 Visual Studio 2012 开发一个 ASP MVC 单页应用程序。它使用由静态 JS/CSS/HTML 文件组成的组件,并根据需要加载。该方法甚至不需要在生产中捆绑,因为静态由浏览器缓存,但在开发中存在问题,因为必须禁用缓存才能刷新我目前正在处理的文件,这意味着大约 100每次页面刷新大约需要 40 秒加载小型静态文件。

我目前正在研究 Chrome 工作区,但我认为更通用的可行解决方案是专门禁用最近 30 分钟内修改日期的文件的缓存。

我正在寻找替代解决方案或现有组件来禁用 VS / ASP(自定义 HTTP 处理程序?)中最近修改的文件的缓存。

4

1 回答 1

-1

假设您使用的是 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 中单个文件的缓存

于 2016-06-25T18:57:08.857 回答