ASP.NET MVC 3 使用页面缓存 OutputCache 需要注意的问题

项目使用MVC3框架,页面使用缓存来缓解服务器压力,使用缓存配置文件设置CacheProfile

  <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);
        }

 

转载于:https://www.cnblogs.com/jeemly/p/4463619.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值