2

我一直在使用会话数组来存储我的 webpart 的状态变量......所以我有一个这样的属性:

    public INode RootNode
    {
        get
        {
            return this.Context.Session["RootNode"] as INode;
        }
        set
        {
            this.Context.Session["RootNode"] = value as object;
        }
    }

这通常工作正常。我发现有时 context.session 变量将为空。

我想知道首先导致会话为空的条件是什么,以及在发生这种情况时保持我的对象的最佳方法是什么?我可以只为上下文分配一个新的 HttpSessionState 对象,还是搞砸了?


编辑:好的,所以它不仅仅是空的会话......整个上下文都搞砸了。当 webpart 进入 init 时,上下文很好......但是当它到达 dropbox selectedindexchange 回发事件(dropbox 包含用于设置 rootnode 变量的节点 ID)时,上下文主要包含 null 属性。

此外,它似乎只在选择某些 id 时发生。这看起来更像是我的某种奇怪的错误,而不是我对会话的理解的问题。

4

2 回答 2

0

I do not know what type of file this is in but I had a similar problem with an .ashx file. The solution was that the handler has to implement IReadOnlySessionState (for read-only access) or IRequiresSessionState (for read-write access). eg:

public class login : IHttpHandler, IRequiresSessionState { ... }

These Interfaces do not need any additional code but act as markers for the framework.

Hope that this helps.

Jonathan

于 2013-04-01T18:20:39.060 回答
0

System.Web.HttpContext.Current.Session

然而,上下文并不取决于类,每说。这取决于正在使用的页面/处理程序的上下文。

-西奥

于 2010-05-28T15:17:46.890 回答