【问题标题】:Caching notification usercontrol缓存通知用户控件
【发布时间】:2018-12-24 12:23:18
【问题描述】:
我正在开发一个 ASP.NET WebForms 项目。我创建了一个通知用户控件(ucNotifications.ascx)。这个用户控件放置在母版页中。通知不会经常更改,所以我想将该用户控件缓存几分钟,尽管缓存的信息很敏感;信息因用户 ID `Session["userid"] 而异。一个用户看不到其他用户的通知。
我read on internet 我应该在我的OutputCache 标记中使用VaryByCustom 属性,并在我的 Global.asax 中实现一个自定义句柄,但我不太明白那个代码。
我该怎么做?如果是这样,这是一个坏主意吗?
【问题讨论】:
标签:
c#
asp.net
caching
webforms
【解决方案1】:
经过一些额外的搜索,我能够解决这个问题:
ucNotifications.ascx
<%@ OutputCache Duration="300" VaryByCustom="userid" VaryByParam="none" %>
Global.asax
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "userid")
{
if (context.Session["userid"] != null)
return context.Session["userid"].ToString();
return string.Empty;
}
return base.GetVaryByCustomString(context, custom);
}