2014-06-19 29 views
1

我的MVC項目之一接近尾聲。我們正在嘗試使用輸出緩存優化項目。MVC:爲所有操作添加輸出緩存

但是,我們發現有很多控制器有更多的操作。我們不認爲將輸出緩存屬性添加到每個動作是一個好主意。

是否有任何解決方案可以將輸出緩存添加到每個操作一次?

+0

全局篩選使用 'BaseController',並繼承了BaseController所有控制器。您可以使用OutputCache作爲適用於所有子控制器的BaseController。這樣你就可以避免爲每個Action指定。 –

回答

2

將其添加到全局過濾器。

filters.Add(new OutputCacheAttribute 
{ 
    NoStore = true, 
    Duration = 0, 
    VaryByParam = "*" 
}); 

可以在App_Start文件夾爲此在FilterConfig.cs文件。

+0

你們都是英雄 –

+0

@JackHe樂於助人...... :) –

0

使用在FilterConfig.cs

public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
     { 

       OutputCacheAttribute cache = new OutputCacheAttribute(); 
        //set other properties 
       filters.Add(cache); 
}