Home >Web Front-end >HTML Tutorial >MVC3 cache (1): page cache_html/css_WEB-ITnose
Enable page caching
If you want to enable page caching in MVC3, you need to add Action is preceded by an OutputCache attribute.
@{ ViewBag.Title = "主页";}<!DOCTYPE html><html><head> <title>页面缓存</title></head><body> 现在时间:@DateTime.Now.ToString("T")</body></html>
[OutputCache(Duration=5, VaryByParam="none")] public ActionResult Index() { return View(); }
The cached location
can be passed Set the cache's Location property to determine where to place the cache.
The properties that can be set by Location are (Any Client Downstream Server None ServerAndClient)
The default value of Location is Any. It is generally recommended to store user information on the Client side and some public information on the Server side.
Added Location it should look like this.
[OutputCache(Duration=5, VaryByParam="none",Location=OutputCacheLocation.Client ,NoStore=true)] public ActionResult Index() { return View(); }
When we need to make unified settings for multiple Actions, we can apply them after unified configuration in the web.config file.
Configure the Caching node in web.config
<caching><outputCacheSettings> <outputCacheProfiles> <add name="Cache1Hour" duration="3600" varyByParam="none"/> </outputCacheProfiles></outputCacheSettings></caching>
[OutputCache(CacheProfile="Cache1Hour")] public ActionResult Index() { return View(); }