標籤:style blog io ar os 使用 sp strong 檔案
啟用頁面緩衝
在MVC3中如果要啟用頁面緩衝,需要在頁面對應的Action前面加上一個OutputCache屬性。
@{ ViewBag.Title = "首頁";}<!DOCTYPE html><html><head> <title>頁面緩衝</title></head><body>現在時間:@DateTime.Now.ToString("T")</body></html>
在Controller中添加對應的Action,並加上OutputCache屬性。
[OutputCache(Duration=5, VaryByParam="none")] public ActionResult Index() { return View(); }
重新整理頁面即可看到頁面做了一個10s的緩衝。當頁面中資料不是需要時時的呈現給使用者時,這樣的頁面緩衝可以減小時時的對資料處理和請求,當然這是針對整個頁面做的緩衝,緩衝的力度還是比較粗的。
緩衝的位置
可以通過設定緩衝的Location屬性,決定將緩衝放置在何處。
Location可以設定的屬性為(Any Client Downstream Server None ServerAndClient)
Location的預設值為Any。一般推薦將使用者的資訊儲存在Client端,一些公用的資訊儲存在Server端。
加上Location應該是這樣的。
[OutputCache(Duration=5, VaryByParam="none",Location=OutputCacheLocation.Client ,NoStore=true)] public ActionResult Index() { return View(); }
設定檔中通用設定
當我們需要對多個Action進行統一的設定時,可以在web.config檔案中統一配置後進行應用即可。
在web.config中配置下Caching節點
<caching><outputCacheSettings> <outputCacheProfiles> <add name="Cache1Hour" duration="3600" varyByParam="none"/> </outputCacheProfiles></outputCacheSettings></caching>
那麼在Action上使用該配置節點即可,這樣的方法對於統一管理配置資訊比較方便。
[OutputCache(CacheProfile="Cache1Hour")] public ActionResult Index() { return View(); }
MVC3緩衝(一):頁面緩衝