2011-04-05 115 views
13

有沒有辦法在ASP.NET中全局禁用服務器緩存?就像在web.config文件中添加某種設置一樣?全局禁用緩存.NET

到目前爲止,我已經嘗試添加這些和它沒有有所作爲......

 <caching> 
      <sqlCacheDependency enabled="false"></sqlCacheDependency> 
      <outputCache enableOutputCache="false" 
       enableFragmentCache="false" 
       sendCacheControlHeader="false" 
       omitVaryStar="false" /> 
     </caching> 
+1

爲什麼你想先禁用它?如果你告訴'爲什麼',那麼你的問題可能會有更好的解決方案。因爲我無法想象緩存服務器本身就是問題所在。如果你不使用它,它不會妨礙你。 – pyrocumulus 2011-04-05 10:13:04

+0

基本上我正在這個網站上工作,我需要添加新的數據庫內容。一旦添加,它將不會顯示在網站上,直到高速緩存結束。 – user441365 2011-04-05 10:24:31

+0

上面的問題是outputCache部分沒有用作緩存中的頂層 – Darren 2015-04-02 07:06:53

回答

0

可以通過刪除其模塊禁用整個應用程序的輸出緩存和會話狀態,這可以從做web.config中

<httpModules> 
    <add name="OutputCache" type="System.Web.Caching.OutputCacheModule" /> 
    <add name="Session" type="System.Web.SessionState.SessionStateModule" /> 
</httpModules> 

添加到您的網頁加載

Response.Cache.SetCacheability(HttpCacheability.NoCache) 
+1

我的web.config上沒有這些模塊並且我已經嘗試在Global.asax中添加Response.Cache.SetCacheability(HttpCacheability.NoCache) ,但沒有運氣 – user441365 2011-04-05 10:28:00

+0

@ user441365 - Response.Cache.SetCacheability(HttpCacheability.NoCache)需要在頁級別添加,而不是在global.asax中 – 2011-04-05 11:38:21

-1

According to MSDN

您可以通過在你的web.config文件中添加enableOutputCache =「假」的OutputCacheSection禁用頁面輸出緩存整個應用程序。

例如,

<configuration> 
    <system.web> 
     <caching> 
      <outputCacheSettings enableOutputCache="false"/> 
     </caching> 
    </system.web> 
</configuration> 

所以你的配置不工作,因爲你有outputCache元素,當它應該是outputCacheSettings元素上的enableOutputCache屬性。

+0

與.NET 3.5和VS2008一起工作,它不喜歡那樣... – user441365 2011-04-05 10:25:53

+0

它給了我一個信息:屬性enableOutputCache沒有聲明 – user441365 2011-04-05 10:31:35

+0

啊,3.5好。是的,你是對的,它似乎已經從outputCache遷移到3.5和4之間的輸出緩存設置 - man - web.config模式是一個可怕的混亂!在這種情況下,你的原稿應該已經工作。一種可能性 - 你使用的是什麼web服務器(vs dev?IIS?什麼版本?) – UpTheCreek 2011-04-05 10:54:49

10

如果您使用IIS7/7.5或IIS Express,還有一種在system.webServer中禁用此功能的方法。這可以在你的主web.config文件(對於webforms和mvc)以及在子文件夾中的web.config文件中工作,以在應用程序的特定區域中禁用它。

<system.webServer> 
    <caching enabled="false" /> 
</system.webServer> 
3

OutputCacheSection部分用於配置應用程序範圍設置,例如啓用還是禁用頁面輸出緩存。例如,您可以通過將enableOutputCache="false"添加到Web.config文件中的OutputCacheSection來禁用整個應用程序的頁面輸出緩存。配置文件中的設置優先於各個頁面中的緩存設置,因此示例設置意味着不會使用輸出緩存。

<system.web> 
     <caching> 
      <outputCache enableOutputCache="false"/>    
     </caching> 
</system.web>