首页 新闻 会员 周边

mvc2 使用outputcache中有关session的问题

0
悬赏园豆:100 [已解决问题] 解决于 2013-11-06 10:17

代码如下
controller中的方法:

 [OutputCache(Duration = 9999,VaryByCustom="aaaa", VaryByParam = "none")]
 public ActionResult method()
 {
  return view();
 }
Global中:

 public override string GetVaryByCustomString(HttpContext context, string custom)
  {
  if (custom=="aaaa")
  {
  return context.Session["aaaa"].ToString();
  }
  return base.GetVaryByCustomString(context, custom);
  }
问题是 context.Session是空的 this.Session 上下文不可用

哎 我折腾一下午了 都不知道怎么弄了 想了很多其他的方法

总之只要达到一个效果 根据我session中的某个值作为缓存的条件
必须是在action哪里进行缓存

问题补充: 还是自己写 cache filter 得了
czd890的主页 czd890 | 专家六级 | 园豆:14412
提问于:2010-12-20 20:28
< >
分享
最佳答案
0

MVC2 使用 ASP.NET 的机制处理缓存,也就是在请求刚开始时就进行判断,查看缓存中是否有当前请求的页面,有则直接返回。

此时(GetVaryByCustomString 方法被调用)Session 还未创建,所以为空。因此在 GetVaryByCustomString 中是无法使用 Session 的。

 

建议使用其它方式,如:VaryByParam,或自己定义新的 缓存 Filter ,难度比较大。

收获园豆:100
鹤冲天 | 老鸟四级 |园豆:2379 | 2010-12-21 12:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册