【问题标题】:ASP.NET Session State Server: Stored Object is NULLASP.NET 会话状态服务器:存储的对象为 NULL
【发布时间】:2015-12-04 17:19:37
【问题描述】:

我已经在 SQL 数据库上设置了一个 ASP.NET 会话状态服务器:

<sessionState timeout="60" allowCustomSqlDatabase="true" mode="SQLServer" sqlConnectionString="..." />

我有一个默认的错误重定向:

<customErrors mode="On" defaultRedirect="~/Error.aspx" />

Global.asax.cs:

private void Application_Error( object sender, EventArgs e )
{
    SomeSessionObj sessionObj = new SomeSessionObj();
    sessionObj.SomeProperty1 = true;
    sessionObj.SomeProperty2 = new Blabla();
    HttpContext.Current.Session["FooBar"] = sessionObj;
}

错误.aspx.cs:

protected void Page_Load( object sender, EventArgs e )
{
    SomeSessionObj sessionObj = HttpContext.Current.Session["FooBar"] as SomeSessionObj;
    // sessionObj is NOT NULL
    // sessionObj.SomeProperty1 is false
    // sessionObj.SomeProperty2 is NULL
}

SomeSessionObj 和 SomeProperty 类都被标记为可序列化。

没有状态服务器(inProc)它可以按预期工作。

提前致谢。

【问题讨论】:

    标签: asp.net session session-state


    【解决方案1】:

    好的,搞定了。

    使用 Session State Server 时,您必须在 Application_Error 中调用 Server.ClearError(),否则 Request 将被终止且 Session 状态不会被写入。

    void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();
        HttpContext.Current.Session["Error"] = ex.Message;
        Response.Redirect("error.aspx",false);
        Server.ClearError();  
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多