【问题标题】:Session Start and Session End in UmbracoUmbraco 中的会话开始和会话结束
【发布时间】:2015-06-28 05:07:55
【问题描述】:

我正在使用 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
  }
}

【问题讨论】:

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


    【解决方案1】:

    你在正确的轨道上,你只需要从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
            }
        }
    }
    

    【讨论】:

    • 我什至无法点击 Init() :/
    • 你的Global.asax中有<%@ Application Codebehind="Global.asax.cs" Inherits="Global" Language="C#" %>吗?
    • <%@ Application Codebehind="Global.asax.cs" Inherits="Umbraco.Web.UmbracoApplication" Language="C#" %>
    • 那是你的问题,应该是Inherits="Global"(全球是你的班级名称)而不是Inherits="Umbraco.Web.UmbracoApplication"
    • 你也必须包含命名空间,所以 Namespace.YourClassName
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 2014-04-22
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多