Global.asax in Umbraco 6
Asked Answered
C

1

9

I had the following in my Global.asax (Umbraco 4.7)

  • Application_Start
  • Application_EndRequest
  • Application_Error
  • Session_Start
  • Session_End

Now I have upgraded to Umbraco 6.0.3, which global.asax inherits from Umbraco.Web.UmbracoApplication

Where do I put my event handlers (and what are the equivalent method names)?

Cheslie answered 8/4, 2013 at 6:39 Comment(0)
C
18

This is what I found so far.

You can create your own class

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)
      {
        // 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
  }
}

And have Global.asax inherit from your class

<%@ Application Codebehind="Global.asax.cs" Inherits="Global" Language="C#" %>

Alternative method: Inherit ApplicationEventHandler - but it's not working for me

Cheslie answered 9/4, 2013 at 3:47 Comment(5)
Could you update me where you have saved Global class in umbraco, and this class name is Global.cs or Global.asax.cs? I know, this is odd, but its not working for me, may be i am missing something.Hexapody
Mine was App_Code/Global.cs but I don't think the filename mattersCheslie
@pink-diamond-square The proper way to edit a post with no language formatting, is to add the correct tag to it. Then the site takes care of formatting automatically. In this case I believe your edit is incorrect, this is not C.Bacon
@Bacon Thanks for the pointer, the code above is C# (and .net XML) but as the question doesn't include code, I figured that it could be answered in any .net language and so changing the tags there alters the meaning of the question.Sinegold
if (Session != null && Session.IsNewSession) this line do not work, how to check for session - I am using umbraco 7Vowell

© 2022 - 2024 — McMap. All rights reserved.