asp.net-mvc - Umbraco 中的 session 开始和 session 结束

标签 asp.net-mvc global-asax umbraco7

我正在使用 umbraco 7.0,我需要在应用程序启动、 session 开始和 session 结束时添加一些自定义代码。至于在umbraco中注册事件,我们必须继承Umbraco.Core.ApplicationEventHandler,我有这样的。但我们只能覆盖 ApplicationStarted 而不能像 Global.asax 中那样覆盖 Session 相关的内容。

我见过Global.asax in Umbraco 6但我无法访问 Session,如该答案 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
  }
}

最佳答案

您的方向是正确的,您只需找到 Session来自 sender 的对象作为 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
        }
    }
}

关于asp.net-mvc - Umbraco 中的 session 开始和 session 结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29765425/

相关文章:

umbraco7 - 如何显示媒体选择器图像

azure - 文件系统是否在多个 Azure 网站之间共享?

c# - 如何将 Web API 添加到现有的 ASP.NET MVC Core 6 Web 应用程序

asp.net-mvc - 如何在 View MVC3中读取cookie?

jquery - 如果存在验证错误是否触发事件?

c# - Global.asax 中的 Server.Transfer

asp.net-mvc - 处理 ASP.NET MVC 中的超时

asp.net - 如何检测请求是否是 Global.asax 中的回调?

c# - Global.asax 中的自动事件连接

jquery - Umbraco7 新后台部分,编辑日期字段,AngularJS