2016-09-23 98 views

回答

5

總之它可以在web.config設置,而不必設置應用到要與使用緩存設置每一個動作或控制器緩存簡檔:

在web.config中指定的選項緩存配置文件:

<system.web> 
    <compilation debug="true" targetFramework="4.5.1" /> 
    <httpRuntime targetFramework="4.5.1" /> 
    <caching> 
     <outputCacheSettings> 
     <outputCacheProfiles> 
      <add name="cp1" duration="30" location="Any" varyByHeader="user-agent" /> 
     </outputCacheProfiles> 
     </outputCacheSettings> 
    </caching> 
    </system.web> 

然後,在任何想要使用配置文件的地方,您都可以使用例如

[OutputCache(CacheProfile="cp1")] 
public ActionResult Index() { 
    //... 
} 

上面的例子取自Apress Pro ASP.NET MVC 5 Platform by Adam Freeman。 我推薦它作爲一個很好的閱讀。

+0

謝謝詹姆斯,你的回答非常清楚。但我不知道如何應用每個動作或控制器的緩存設置。也許這不是需要的信息,但我想看一個例子。再次感謝你。 – Orhun

+0

我發現這篇博客文章(https://prashantbrall.wordpress.com/2013/09/29/using-output-cache-profile-in-asp-net-mvc/)也許你可以看看,它是回答我的第二個問題。再次感謝您的關注。 – Orhun