asp.net-mvc – MVC3自定义输出缓存
发布时间:2020-12-16 03:37:16 所属栏目:asp.Net 来源:网络整理
导读:我想在我的应用程序中使用缓存,但我返回的数据特定于登录用户.当我需要因用户而异时,我无法使用任何现成的缓存规则. 有人可以指出我在创建自定义缓存属性方面的正确方向.从控制器我可以从Thread.CurrentPrincipal.Identity访问用户;或者我在控制器构造函数_u
我想在我的应用程序中使用缓存,但我返回的数据特定于登录用户.当我需要因用户而异时,我无法使用任何现成的缓存规则.
有人可以指出我在创建自定义缓存属性方面的正确方向.从控制器我可以从Thread.CurrentPrincipal.Identity访问用户;或者我在控制器构造函数_user中初始化的私有控制器成员 谢谢. 解决方法
你可以使用VaryByCustom.在Global.asax中重写GetVaryByCustomString方法:
public override string GetVaryByCustomString(HttpContext context,string arg) { if (arg == "IsLoggedIn") { if (context.Request.Cookies["anon"] != null) { if (context.Request.Cookies["anon"].Value == "false") { return "auth"; } else { return "anon"; } } else { return "anon"; } } else { return base.GetVaryByCustomString(context,arg); } } 然后使用OutputCache属性: [OutputCache(CacheProfile = "MyProfile")] public ActionResult Index() { return View(); } 并在web.config中: <caching> <outputcachesettings> <outputcacheprofiles> <clear /> <add varybycustom="IsLoggedIn" varybyparam="*" duration="86400" name="MyProfile" /> </outputcacheprofiles> </outputcachesettings> </caching> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 如何更新实体框架连接字符串
- asp.net – AppFabric Cache连接类型和协议
- asp.net – 强制IIS快速到经典管道模式
- 如何在ASP.NET和C#中每个会话“临时”存储Web服务器上的图像
- ASP.Net OData在LB中通过SSL终止失败
- 在ASP.Net动态数据中使用Computed Property作为DisplayColu
- asp.net – 如何修复“’ddlAssignedTo’有一个无效的Selec
- asp.net-mvc-3 – 使用Ajax.BeginForm工作的mvc 3局部视图(
- asp.net-mvc-4 – Asp.NET MVC – DataAnnotations和ModelS
- asp.net-mvc-4 – 值“(字符串)”无效
推荐文章
站长推荐
- asp.net-mvc-3 – 无法将带有[]的索引应用于ICol
- asp.net – “此操作需要IIS集成管道模式
- asp.net-mvc – 如何在MVC5中从AccountControlle
- 如何在ASP.net站点中重写URL
- asp.net – 带CSS的Control.ClientID
- asp.net-mvc – 实体框架 – 选择特定列并返回强
- asp.net-mvc – 为什么HttpContext.Current在asp
- asp.net – SignalR 2.0超时连接
- asp.net – DirectoryServicesCOMException 8007
- asp.net-mvc – 使用ASP.NET MVC的IIS应用程序请
热点阅读