缓存通知用户控件

时间:2018-07-16 21:49:12

标签: c# asp.net caching webforms

我正在研究ASP.NET WebForms项目。我创建了一个通知用户控件(ucNotifications.ascx)。此用户控件位于母版页中。通知不会经常更改,因此我想将该用户控件缓存几分钟,不过,缓存的信息是敏感的;该信息因用户ID Session [“ userid”]而异。一个用户看不到其他人的通知。

read on internet应该在VaryByCustom标签中使用OutputCache属性,并在Global.asax中实现自定义句柄,但是我不太理解该代码

我该怎么办?如果这样,那是个坏主意吗?

1 个答案:

答案 0 :(得分:0)

经过一些额外的搜索,我得以解决此问题:

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);
}