全局禁用缓存.NET

时间:2011-04-05 10:09:45

标签: .net caching

有没有办法在ASP.NET中全局禁用服务器缓存?比如在web.config文件中添加某种设置?

到目前为止,我已尝试添加这些并且它没有任何区别......

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

4 个答案:

答案 0 :(得分:13)

如果您使用的是IIS7 / 7.5或IIS Express,还有一种在system.webServer中禁用此功能的方法。这将在您的主web.config文件(对于webforms和mvc)以及子文件夹中的web.config文件中工作,以便为应用程序的特定区域禁用它。

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

答案 1 :(得分:3)

OutputCacheSection部分用于配置应用程序范围设置,例如是启用还是禁用页面输出缓存。例如,您可以通过将enableOutputCache="false"添加到Web.config文件中的OutputCacheSection来禁用整个应用程序的页面输出缓存。配置文件中的设置优先于各个页面中的缓存设置,因此示例设置意味着不会使用输出缓存。

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

答案 2 :(得分: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)

答案 3 :(得分:-1)

According to MSDN

  

您可以通过将enableOutputCache =“false”添加到Web.config文件中的OutputCacheSection来禁用整个应用程序的页面输出缓存。

e.g。

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

所以您的配置无法正常运行,因为enableOutputCache元素上的outputCache属性应该位于outputCacheSettings元素上。

相关问题