Umbraco 中的会话开始和会话结束

Session Start and Session End in Umbraco

我正在使用 umbraco 7.0,我需要在应用程序启动、会话启动和会话结束时添加一些自定义代码。至于在 umbraco 中注册事件,我们必须从 Umbraco.Core.ApplicationEventHandler 继承,我已经这样做了。但是我们只能覆盖 ApplicationStarted 而不是 Session 相关的,就像我们在 Global.asax.

中可以做的那样

我已经看到 Global.asax in Umbraco 6 但我无法访问该答案中显示的会话 if (Session != null && Session.IsNewSession),也许是 umbraco 6 和 umbraco 7 中的一些更改。

有什么解决办法吗?

这是post中建议的代码。

public class Global : Umbraco.Web.UmbracoApplication
{
  public void Init(HttpApplication application)
  {
    application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);
    application.EndRequest += (new EventHandler(this.Application_EndRequest));
    //application.Error += new EventHandler(Application_Error); // Overriding this below
  }

  protected override void OnApplicationStarted(object sender, EventArgs e)
  {
    base.OnApplicationStarted(sender, e);
    // Your code here
  }

  private void application_PreRequestHandlerExecute(object sender, EventArgs e)
  {
    try
    {
      if (Session != null && Session.IsNewSession) // Not working for me
      {
        // Your code here
      }
    }
    catch(Exception ex) { }
  }

  private void Application_BeginRequest(object sender, EventArgs e)
  {
    try { UmbracoFunctions.RenderCustomTree(typeof(CustomTree_Manage), "manage"); }
    catch { }
  }

  private void Application_EndRequest(object sender, EventArgs e)
  {
    // Your code here
  }

  protected new void Application_Error(object sender, EventArgs e)
  {
    // Your error handling here
  }
}

您的方向是正确的,您只需要从作为参数传递给您的 sender 中找到 Session 对象 PreRequestHandlerExecute.

public class Global : UmbracoApplication
{
    public override void Init()
    {
        var application = this as HttpApplication;
        application.PreRequestHandlerExecute += PreRequestHandlerExecute;
        base.Init();
    }

    private void PreRequestHandlerExecute(object sender, EventArgs e)
    {
        var session = ((UmbracoApplication)sender).Context.Session;
        if (session != null && session.IsNewSession)
        {
            // Your code here
        }
    }
}