Asp.net mvc 3 outputcache mvc3仅注销用户缓存
是否有一种方法可以使用OutputCache属性缓存仅已注销用户的结果并重新评估已登录用户的结果示例: 我想要什么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(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,这将使每个请求都是唯一的。但这会对性能产生一些影响。