代码如下
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哪里进行缓存
MVC2 使用 ASP.NET 的机制处理缓存,也就是在请求刚开始时就进行判断,查看缓存中是否有当前请求的页面,有则直接返回。
此时(GetVaryByCustomString 方法被调用)Session 还未创建,所以为空。因此在 GetVaryByCustomString 中是无法使用 Session 的。
建议使用其它方式,如:VaryByParam,或自己定义新的 缓存 Filter ,难度比较大。