2009-10-01 70 views
7

我有一個虛擬路徑提供程序。問題是它緩存我的文件。無論何時我手動編輯其中一個aspx文件,它引用的VPP都不會引入新文件,它會繼續重新使用舊文件,直到我重新啓動該網站。虛擬路徑提供程序禁用緩存?

我甚至在我的VirtualPathProvider類過度騎着GetCacheDependency():

public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) 
    { 
     return null; 
    } 

想法?

回答

19

返回null基本上告訴ASP.NET你沒有任何依賴 - 因此ASP.NET不會重新加載項目。

你需要的是返回一個有效的依賴關係,例如

public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) 
    { 
     return new CacheDependency(getPhysicalFileName(virtualPath)); 
    } 

一個更正確的做法是,以確保您只處理自己的緩存依賴(這是一個示意性的例子):

public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) 
    { 
     if (isMyVirtualPath(virtualPath)) 
      return new CacheDependency(getPhysicalFileName(virtualPath)); 
     else 
      return new Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart); 
    } 
+1

更進一步的正確答案/ Chandima Prematillake – r3mark 2013-06-11 04:33:24

1
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) 
{ 
    return IsVirtualPath(virtualPath) ? new CacheDependency(HttpContext.Current.Server.MapPath("~/Resource.xml")) 
            : Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart); 
} 
2

我不相信這是個什麼原始海報問。他希望完全禁用緩存,而不是以更好的方式實現緩存,儘管您的文章對後者有幫助。

很多人使用VirtualPathProvider從數據庫中取出數據,而不是從文件系統中取出數據。我沒有看到如何創建文件系統依賴關係是確定何時刷新文件的有用方法。

你會如何強制它永遠不會使用緩存並始終檢索最新版本的文件?就是那個問題。

+0

回覆此處:) http://stackoverflow.com/questions/3747858/asp-net-mvc-2-virtualpathprovider-getfile-every-time-for-every-請求/ 3766321#3766321 – Aliz 2015-02-13 11:19:47

14

禁用緩存正確的方法是這樣的:

public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) 
    { 
     if (_IsLayoutFile(virtualPath)) 
     { 
      return null; 
     } 
     return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart); 
    } 

    public override String GetFileHash(String virtualPath, IEnumerable virtualPathDependencies) 
    { 
     if (_IsLayoutFile(virtualPath)) 
     { 
      return Guid.NewGuid().ToString(); 
     } 

     return Previous.GetFileHash(virtualPath, virtualPathDependencies); 
    } 
+0

根據假設,返回null緩存依賴將強制VPP使用getfilehash? – 2013-11-06 08:44:09

+0

我想通了,這是真的:返回一個null CacheDependecy將強制VPP使用GetFileHash。 – 2014-03-08 06:32:11

-1

到,根據要求爲我工作的解決方案是:

  • GetCacheDependency:返回NULL;
  • GetFileHash: return Guid.NewGuid()。ToString();

但是,使用此解決方案導致掛起服務器(Cassini,IIS 6,IIS 7,IIS 8)。吊只持續幾分鐘,然後結果交付。

我還包括一個測試虛擬路徑/文件與相同的結果。我迷惑瀏覽器超時。

任何人都可以幫忙嗎?

+0

你應該問一個新問題。不要在答案中提問。 – Difster 2017-07-25 23:10:44

+0

好。在這裏問:https:// stackoverflow。COM /問題/ 45320336 /的VirtualPathProvider-禁用緩存,掛起服務器-IIS-和卡西尼 – jbecker 2017-07-28 02:14:07