2016-07-06 21 views
4

我想解決瀏覽器緩存問題。每當js和css文件發生任何變化時,這些文件都是從瀏覽器緩存中提供的,而不是從服務器提供的,我在互聯網上重新搜索,發現來自krinstinsen的this great postASP.Net緩存清除方法不起作用(更新)

我在我的App_Code文件夾中的類中包含了以下類和方法。

using System; 
using System.IO; 
using System.Web; 
using System.Web.Caching; 
using System.Web.Hosting; 

public class Fingerprint 
{ 
    public static string Tag(string rootRelativePath) 
    { 
    if (HttpRuntime.Cache[rootRelativePath] == null) 
    { 
     string absolute = HostingEnvironment.MapPath("~" + rootRelativePath); 

     DateTime date = File.GetLastWriteTime(absolute); 
     int index = rootRelativePath.LastIndexOf('/'); 

     string result = rootRelativePath.Insert(index, "/v-" + date.Ticks); 
     HttpRuntime.Cache.Insert(rootRelativePath, result, new CacheDependency(absolute)); 
    } 

     return HttpRuntime.Cache[rootRelativePath] as string; 
    } 
} 

後來我改變了所有我的aspx頁面(近500個位置)的引用,像下面。

<script type="text/javascript" src="<%=Fingerprint.Tag("/Scripts/JQuery/jquery.color.js")%>"></script> 
    <script type="text/javascript" src="<%=Fingerprint.Tag("/Scripts/JQuery/jquery.glowbuttons.js?v=1.1")%>"></script> 

建議我還在IIS中添加了以下重寫規則並安裝了重寫模塊。

<rewrite> 
     <rules> 
     <rule name="fingerprint"> 
      <match url="([\S]+)(/v-[0-9]+/)([\S]+)" /> 
      <action type="Rewrite" url="{R:1}/{R:3}" /> 
     </rule> 
     </rules> 
    </rewrite> 

現在的問題我面臨的

這一切都在我的開發環境中工作了魅力。當我將代碼發佈到iis(uat和我的本地iis)時,相同的代碼無法工作。

Fingerprint.Tag()方法返回錯誤的URL。

我發展的URL是這樣下面

http://localhost:54992/login.aspx 

我的IIS網站的網址是這樣下面

http://www.example.com/myClientName/login.aspx 

您可能已經注意到URL段(\myClientName\)的IIS上一個額外的層面上,這是導致問題的原因。

我還添加了邏輯,以在URL中添加myClientName部分,但不幸的是,這也不起作用。

在IIS託管的網站上我充斥着404錯誤,因爲url路徑跳過了myClientName部分。

UPDATE 1

我還與以下相同的方法的另一個版本中,如果代碼是在iisexpress或在Full IIS相應地運行併產生路徑

public static string Tag(string rootRelativePath) 
    { 

     if (rootRelativePath.Contains("~")) 
      rootRelativePath = rootRelativePath.Replace("~", string.Empty); 

     bool isRunningInIisExpress = Process.GetCurrentProcess() 
           .ProcessName.ToLower().Contains("iisexpress"); 

     if (HttpRuntime.Cache[rootRelativePath] == null) 
     { 
      string siteAlias = string.Empty; 

      if (!string.IsNullOrEmpty(WebConfigurationManager.AppSettings["SiteName"])) 
       siteAlias = WebConfigurationManager.AppSettings["SiteName"].ToString(); 
      string absolute = HostingEnvironment.MapPath("~" + rootRelativePath); 

      DateTime date = File.GetLastWriteTime(absolute); 
      int index = rootRelativePath.LastIndexOf('/'); 

      string result = rootRelativePath.Insert(index, "/v-" + date.Ticks); 
      if (!isRunningInIisExpress) 
      { 
       siteAlias = "/" + siteAlias; 
       result = siteAlias + result; 
      } 
      if (File.Exists(absolute)) 
       HttpRuntime.Cache.Insert(rootRelativePath, result, new CacheDependency(absolute)); 
     } 

     return HttpRuntime.Cache[rootRelativePath] as string; 
    } 

,檢查嘗試過請指導我正確的方向,任何指向我做錯了什麼。

+0

如果你發現重寫規則會發生什麼?將在開發上的開發路徑相同? – Dexion

+0

@Dexion是否類似於 Devjosh

+0

是的。我建議比較iis /站點配置。 – Dexion

回答

1

使用相對路徑或將站點設置爲不同的端口,因此它將在live env中以與dev env相同的方式運行。

+0

對參考文獻進行更改是一件很乏味的工作,因爲可能會說90/100頁,而每個頁面都有CSS和js參考。 我不允許在不同的端口上運行該網站。謝謝 – Devjosh