MVC3緩衝(一):頁面緩衝

來源:互聯網
上載者:User

標籤: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緩衝(一):頁面緩衝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.