启用页面缓存
在MVC3中如果要启用页面缓存,需要在页面对应的Action前面加上一个OutputCache属性。
立即学习“前端免费学习笔记(深入)”;
@{ 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(); }立即学习“前端免费学习笔记(深入)”;
缓存的位置
可以通过设置缓存的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>
立即学习“前端免费学习笔记(深入)”;
立即学习“前端免费学习笔记(深入)”;
[OutputCache(CacheProfile="Cache1Hour")] public ActionResult Index() { return View(); }立即学习“前端免费学习笔记(深入)”;
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号