会话开始和会话结束的一把umbraco [英] Session Start and Session End in Umbraco

查看:225
本文介绍了会话开始和会话结束的一把umbraco的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一把umbraco 7.0,我需要添加一些自定义code在应用程序启动,会话开始和会话结束。至于在一把umbraco注册事件,我们必须从 Umbraco.Core.ApplicationEventHandler 继承,我已经这么曾根。但是,我们只能重写,因为我们可以在Global.asax中做 ApplicationStarted ,而不是会话相关的。

我在一把umbraco看到的Global.asax 6 但我不能访问会话如图这个问题的答案如果(会话= NULL&放大器;!&安培; Session.IsNewSession)。,也许是一把umbraco 6和东西一把umbraco 7改为

任何解决方案?

继承人的code中提到的建议后

 公共类全球:Umbraco.Web.UmbracoApplication
{
  公共无效初始化(HttpApplication的应用程序)
  {
    。应用preRequestHandlerExecute + =新的EventHandler(application_ preRequestHandlerExecute);
    application.EndRequest + =(新的EventHandler(this.Application_EndRequest));
    //application.Error + =新的EventHandler(的Application_Error); //覆盖此下方
  }  保护覆盖无效OnApplicationStarted(对象发件人,EventArgs的发送)
  {
    base.OnApplicationStarted(发件人,E);
    //你的code在这里
  }  私人无效application_ preRequestHandlerExecute(对象发件人,EventArgs的发送)
  {
    尝试
    {
      如果(会话= NULL&放大器;!&安培; Session.IsNewSession)//不是为我工作
      {
        //你的code在这里
      }
    }
    赶上(例外前){}
  }  私人无效的Application_BeginRequest(对象发件人,EventArgs的发送)
  {
    尝试{UmbracoFunctions.RenderCustomTree(typeof运算(CustomTree_Manage),管理); }
    赶上{}
  }  私人无效Application_EndRequest(对象发件人,EventArgs的发送)
  {
    //你的code在这里
  }  保护新无效的Application_Error(对象发件人,EventArgs的发送)
  {
    //你的错误处理
  }
}


解决方案

您在正确的轨道上,你只需要找到从<$ c中的会话对象$ C>发件人传递给你的论点 preRequestHandlerExecute

 公共类全球:UmbracoApplication
{
    公共覆盖无效的init()
    {
        VAR应用=这是HttpApplication的;
        应用preRequestHandlerExecute + = preRequestHandlerExecute。
        base.Init();
    }    私人无效preRequestHandlerExecute(对象发件人,EventArgs的发送)
    {
        VAR会话=((UmbracoApplication)发送方).Context.Session;
        如果(会话= NULL&放大器;!&安培; session.IsNewSession)
        {
            //你的code在这里
        }
    }
}

I am using umbraco 7.0 and I need to add some custom code on application start, session start and session end. As for registering events in umbraco we have to inherit from Umbraco.Core.ApplicationEventHandler, I have sone so. But in that we can only override ApplicationStarted and not Session related as we can do in Global.asax.

I have seen Global.asax in Umbraco 6 but I can not access Session as shown in that answer if (Session != null && Session.IsNewSession), maybe it was umbraco 6 and something changed in umbraco 7.

Any solutions?

Heres the code suggested in 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
  }
}

解决方案

You are on the right track, you just need to find the Session object from the sender that is passed to you as the 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
        }
    }
}

这篇关于会话开始和会话结束的一把umbraco的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆