ASP.NET MVC 3 使用页面缓存 OutputCache 需要注意的问题
2015-04-28 17:38
761 查看
项目使用MVC3框架,页面使用缓存来缓解服务器压力,使用缓存配置文件设置CacheProfile
在Action上使用OutputCache特性
配置和代码完成以后,运行页面。首次页面返回200状态,按 F5后页面还是返回200的状态码,怎么回事呢,页面居然没有被缓存。。。。
原来这是ASP.NET的一个BUG,如何解决呢?我们可以在Action方法内加 Response.Cache.SetOmitVaryStar(true);这段代码。测试发现配置信息里的属性location配置为"Client"时,缓存并没有生效,页面还是无法被缓存。。。。这个太坑了吧。
将配置信息里的属性location配置为"ServerAndClient"时,页面才能被缓存。
<system.web> ...........<!--其他配置节点--> <caching> <outputCacheSettings> <outputCacheProfiles> <add name="index" duration="20" enabled="true" location="Client" varyByParam="city,type"/> </outputCacheProfiles> </outputCacheSettings> </caching> </system.web>
在Action上使用OutputCache特性
[OutputCache(CacheProfile="index")] public ActionResult Index(string city, string type) { ViewBag.City = city; ViewBag.Type = type; ViewBag.Message = "欢迎使用 ASP.NET MVC!"; var queryList = list.Where(c => c.City == city); return View(queryList); }
配置和代码完成以后,运行页面。首次页面返回200状态,按 F5后页面还是返回200的状态码,怎么回事呢,页面居然没有被缓存。。。。
原来这是ASP.NET的一个BUG,如何解决呢?我们可以在Action方法内加 Response.Cache.SetOmitVaryStar(true);这段代码。测试发现配置信息里的属性location配置为"Client"时,缓存并没有生效,页面还是无法被缓存。。。。这个太坑了吧。
将配置信息里的属性location配置为"ServerAndClient"时,页面才能被缓存。
<system.web> ...........<!--其他配置节点--> <caching> <outputCacheSettings> <outputCacheProfiles> <add name="index" duration="20" enabled="true" location="ServerAndClient" varyByParam="city,type"/> </outputCacheProfiles> </outputCacheSettings> </caching> </system.web>
public ActionResult Index(string city, string type) { Response.Cache.SetOmitVaryStar(true); ViewBag.City = city; ViewBag.Type = type; ViewBag.Message = "欢迎使用 ASP.NET MVC!"; var queryList = list.Where(c => c.City == city); return View(queryList); }
相关文章推荐
- ASP.NET MVC中使用OutputCache实现服务器端页面级缓存注意问题
- 在ASP.NET MVC 3 中自定义AuthorizeAttribute时需要注意的页面缓存问题
- 在ASP.NET MVC 3 中自定义AuthorizeAttribute时需要注意的页面缓存问题
- ASP.NET MVC 使用Swagger需要注意的问题!!!
- 注意!ASP.NET MVC 3 的一个 OutputCache 问题
- 注意!ASP.NET MVC 3 的一个 OutputCache 问题
- ASP.NET MVC中使用jQuery时的浏览器缓存问题详解
- ASP.NET MVC中使用jQuery时的浏览器缓存问题详解
- ASP.NET MVC 页面使用富文本控件的XSS漏洞问题
- ASP.NET MVC 页面使用富文本控件的XSS漏洞问题
- ASP.NET MVC中使用jQuery时的浏览器缓存问题
- 在asp.net页面中使用DIV定位需要注意的
- ASP.NET MVC中重写IAuthorizationFilter的需要注意的问题
- ASP.NET MVC3缓存之一:使用页面缓存
- 大叔手记(20):ASP.NET MVC中使用jQuery时的浏览器缓存问题
- ASP.NET MVC中使用jQuery时的浏览器缓存问题
- 注意!ASP.NET MVC 3 的一个 OutputCache 问题
- 注意!ASP.NET MVC 3 的一个 OutputCache 问题
- 勤于思考: ASP.NET MVC 注销后 使用浏览器 【后退】 不使用缓存页面
- ASP.NET MVC中使用jQuery时的浏览器缓存问题