【问题标题】:Kentico 11 Accessing Session Variables on Session EndKentico 11 在会话结束时访问会话变量
【发布时间】:2022-02-11 01:45:01
【问题描述】:

我们正在尝试在会话结束时访问会话变量。我们创建了一个名为 ApplicationSessionEventsHandlerModule 的新模块并添加了一个 SessionEnd_Execute 处理程序。

但是,Kentico SessionHelper 类不起作用,System.Web.HttpContext.Current.Session 也不起作用。您如何建议我们在会话结束时访问会话变量?见代码:

  public class ApplicationSessionEventsHandlerModule : Module
    {
    public ApplicationSessionEventsHandlerModule() :
        base("ApplicationSessionEventsHandlerModule")
        {
        }

    protected override void OnInit()
        {
        base.OnInit();
        ApplicationEvents.SessionStart.Execute += SessionStart_Execute;
        ApplicationEvents.SessionEnd.Execute += SessionEnd_Execute;
        }

    private void SessionEnd_Execute(object sender, EventArgs e)
        {

        if (SessionHelper.SessionIsAvailable)
            {
            if (SessionHelper.GetValue("UserDetails") != null)
                {
               //Do stuff
                    }
                }

            }


    private void SessionStart_Execute(object sender, EventArgs e)
        {

        if (SessionHelper.GetValue("UserDetails") != null)
            {
          //do stuff
            }
        }
    }

【问题讨论】:

    标签: session kentico


    【解决方案1】:

    参考链接 - https://docs.xperience.io/k11/custom-development/handling-global-events/reference-global-system-events#ReferenceGlobalsystemevents-ApplicationEvents

    请尝试以下 -

    1. 在\App_Code\下创建一个全局文件夹

    2. 在 Global 文件夹下创建一个 ApplicationSessionEventsHandler.cs 文件,代码如下 sn -p 并尝试 -

       using CMS.Base;
       using CMS.DocumentEngine;
       using CMS.Helpers;
       using CMS.Membership;
      
       [ApplicationSessionEventsHandler]
       public partial class CMSModuleLoader
       {
       public class ApplicationSessionEventsHandler : CMSLoaderAttribute
       {
       public override void Init()
       {
       ApplicationEvents.SessionEnd.Execute += SessionEnd_Execute;
       ApplicationEvents.SessionStart.Execute += SessionStart_Execute;
      
       }
      
       private void SessionStart_Execute(object sender, System.EventArgs e)
       {
       if (SessionHelper.GetValue("UserDetails") != null)
       {
       //do stuff
       }
       }
       private void SessionEnd_Execute(object sender, System.EventArgs e)
       {
       if (SessionHelper.SessionIsAvailable)
       {
       if (SessionHelper.GetValue("UserDetails") != null)
       {
       //Do stuff
       }
       }
       }
       }
       }
      

    【讨论】:

    • 我试过这个,但似乎 CMSLoaderAttribute 类不存在。 v11 有更新的解决方案吗?
    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    相关资源
    最近更新 更多