Session start and session end in Umbraco

I am using umbraco 7.0 and I need to add some code to start the application, start a session, and end a session. With regards to logging events in umbraco, we have to inherit from Umbraco.Core.ApplicationEventHandler

, I did that. But in this we can only override ApplicationStarted

, not session bound, as we can do in Global.asax.

I have seen Global.asax in Umbraco 6 but I cannot access the session as shown in this answer if (Session != null && Session.IsNewSession)

, it may have been umbraco 6 and something changed in umbraco 7.

Any solutions?

Here is the code suggested in the mentioned 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
  }
}

      

+3


source to share


1 answer


You are on the right track, you just need to find the object Session

from sender

which is passed to you as an argument in 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
        }
    }
}

      

+6


source







All Articles