Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 outputcache mvc3仅注销用户缓存_Asp.net Mvc 3_Outputcache - Fatal编程技术网

Asp.net mvc 3 outputcache mvc3仅注销用户缓存

Asp.net mvc 3 outputcache mvc3仅注销用户缓存,asp.net-mvc-3,outputcache,Asp.net Mvc 3,Outputcache,是否有一种方法可以使用OutputCache属性缓存仅已注销用户的结果并重新评估已登录用户的结果示例: 我想要什么 [OutputCache(onlycacheanon = true)] public ActionResult GetPhoto(id){ var photo = getPhoto(id); if(!photo.issecured){ return photo... } return getPhotoOnlyIfCurrentUserHasAcc

是否有一种方法可以使用OutputCache属性缓存仅已注销用户的结果并重新评估已登录用户的结果示例:

我想要什么

[OutputCache(onlycacheanon = true)]
public ActionResult GetPhoto(id){
   var photo = getPhoto(id);
   if(!photo.issecured){
      return photo...
   }
   return getPhotoOnlyIfCurrentUserHasAccess(id);
   //otherwise return default photo so please don't cache me
}

您可以使用
[OutputCache]
中的
VaryByCustom
属性

然后重写并检查
HttpContext.Current.User.IsAuthenticated

  • 如果未经过身份验证(激活缓存),则返回“未授权”或类似信息
  • Guid.NewGuid().ToString()
    使缓存无效

    • 我就是这样实现上述功能的

      在Global.asax.cs中:

      public override string GetVaryByCustomString(HttpContext context, string custom)
      {
          if (custom == "UserId")
          {
              if (context.Request.IsAuthenticated)
              {
                  return context.User.Identity.Name;
              }
              return null;
          }
      
          return base.GetVaryByCustomString(context, custom);
      }
      
      输出缓存属性中的用法:

       [OutputCache(Duration = 30, VaryByCustom = "UserId" ...
       public ActionResult MyController()
       {
          ...
      

      这正是我错过的东西谢谢你。我没有意识到空禁用缓存。不知道它是可用的。谢谢:)
      null
      不会禁用缓存。我在示例项目上尝试过该解决方案,它只是为经过身份验证和未经身份验证的用户分别缓存…@Cheburek您可以返回Guid.NewGuid().ToString()而不是null,这将使每个请求都是唯一的。但这会对性能产生一些影响。