2014-10-28 35 views
12

我有一個單頁面應用程序(angular-js)通過IIS提供服務。如何防止HTML文件的緩存?該解決方案需要通過更改index.html或web.config中的內容來實現,因爲無法通過管理控制檯訪問IIS。如何禁用通過IIS提供的單頁應用程序HTML文件的緩存?

我目前正在調查某些選項:

IIS是版本7.5與.NET框架4

+0

那麼,什麼是問題 – harishr 2014-10-28 05:03:12

+0

問號爲你添加。 – Andrew 2014-10-28 05:06:28

+0

但是,您已經在您提供的鏈接中找到了答案...鏈接告訴我如何在不觸摸iis的情況下禁用緩存,那麼您還想知道什麼 – harishr 2014-10-28 05:08:56

回答

23

添加以下內容的web.config解決跨瀏覽器,IE,Firefox和Safari的工作:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 

    <location path="index.html"> 
    <system.webServer> 
     <httpProtocol> 
     <customHeaders> 
      <add name="Cache-Control" value="no-cache" /> 
     </customHeaders> 
     </httpProtocol> 
    </system.webServer> 
    </location> 

</configuration> 

這將確保請求index.html時的那Cache-Control頭被設置爲no-cache

+14

我認爲這隻在點擊一個直接包含index.html的url時纔有效.. SPA中的所有請求都具有虛擬URL並且不映射到真實位置路徑。有什麼可以做的呢? – 2015-08-27 17:50:39

5

當您提供您的html文件時,您可以附加一個隨機查詢字符串。這將阻止瀏覽器使用舊版本,即使該文件位於瀏覽器緩存中。

/index.html?rnd=timestamp 

另一個選項是在IIS級別添加no-cache設置。這增加了Cache-Control:響應中的no-cache,它告訴瀏覽器不緩存文件。它從IIS 7開始工作。

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <!-- Note the use of the 'location' tag to specify which 
     folder this applies to--> 
    <location path="index.html"> 
    <system.webServer> 
     <staticContent> 
     <clientCache cacheControlMode="DisableCache" /> 
     </staticContent> 
    </system.webServer> 
    </location> 
</configuration> 
+0

關於向URL添加時間戳 - 是的,但我認爲這是更多的臨時黑客而不是解決方案。我不相信任何有信譽的SPA解決方案都使用這種方法。 – Andrew 2014-10-28 04:34:04

+0

好吧,添加了如何使用IIS本身做到這一點。 – govin 2014-10-28 04:36:01

+3

此解決方案不適用於Google Chrome。 – Andrew 2014-10-31 02:04:10

4

對於.NET Core,我使用了以下內容。

 app.UseStaticFiles(new StaticFileOptions 
     { 
      OnPrepareResponse = context => 
      {     
       if (context.File.Name == "index.html") { 
        context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store"); 
        context.Context.Response.Headers.Add("Expires", "-1"); 
       } 
      } 
     }); 

感謝How to disable browser cache in ASP.NET core rc2?

相關問題