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

时间:2015-04-21 07:17:28

标签: asp.net-mvc global-asax umbraco7

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

我见过Global.asax in Umbraco 6但我无法访问会话,如答案if (Session != null && Session.IsNewSession)所示,也许是umbraco 6,umbraco 7中的内容发生了变化。

任何解决方案?

下面提到的帖子中提到的代码。

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

1 个答案:

答案 0 :(得分:6)

您走在正确的轨道上,您只需要找到Session中的sender对象作为[{1}}中的参数传递给您。

PreRequestHandlerExecute
相关问题