2017-07-20 21 views
0

如何緩存僅圖像而不是Javascript和Css文件?Asp.Net緩存但不支持javascript的圖像

是否有任何類型的頭控制哪些mimetype要緩存?

添加一些進一步的解釋:我的global.asax.cs中有以下代碼片段,這樣在我的開發環境中,我不會緩存JavaScript文件(或者需要我每次重新啓動服務器一個簡單的改變)。由於圖像很少變化,我不知道有沒有辦法。

if (ApplicationConfiguration.IsLocal()) { 
      //do not cache content locally, to make development faster 
      Response.Cache.SetCacheability(HttpCacheability.NoCache); 
      Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1)); 
      Response.Cache.SetNoStore(); 
     } 

感謝

回答

0

最終事實證明是一個錯誤rookie's:

我有(錯誤的)印象,將有來自服務器只有一個響應,同時,作爲事實上,每個資源都有幾個,一個。

因此,我只需要創建一個額外的條件,以驗證是否需要對響應給定的資源或不被緩存:

if (ApplicationConfiguration.IsLocal() && !ShouldCache()) { 
      //do not cache content locally, to make development faster 
      Response.Cache.SetCacheability(HttpCacheability.NoCache); 
      Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1)); 
      Response.Cache.SetNoStore(); 
     } 


private bool ShouldCache() { 
     if (Request.AppRelativeCurrentExecutionFilePath == null) { 
      return false; 
     } 

     return Request.AppRelativeCurrentExecutionFilePath.IndexOf("vendor",StringComparison.CurrentCultureIgnoreCase) !=-1 
       || Request.AppRelativeCurrentExecutionFilePath.IndexOf(".png", StringComparison.CurrentCultureIgnoreCase) != -1 
       || Request.AppRelativeCurrentExecutionFilePath.IndexOf(".jpg", StringComparison.CurrentCultureIgnoreCase) != -1; 
    } 

在這種情況下,我一直緩存任何圖像和啓用我的「供應商」腳本(第三方)