Web 部件 context.session 为 null

发布于 2024-08-26 05:17:34 字数 676 浏览 6 评论 0原文

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

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

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

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


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

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

I've been using the session array to store a state variable for my webpart... so I have a property like this:

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

This usually works fine. I've discovered that sometimes, the context.session variable will be null.

I'd like to know what are the conditions that cause the session to be null in the first place, and whats the best way to persist my object when this happens? Can I just assign a new HttpSessionState object to the context, or does that screw things up?


Edit: Ok, so its not just the session that is null... the whole context is screwed up. When the webpart enters the init, the context is fine... but when it reaches the dropbox selectedindexchange postback event (the dropbox contains node id's to use to set the rootnode variable), the context contains mostly null properties.

also, it only seems to happen when certain id's are selected. This looks more like some kind of weird bug on my end than a problem with my understanding of the session.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疧_╮線 2024-09-02 05:17:34

System.Web.HttpContext.Current.Session

但是,上下文并不依赖于类。这取决于正在使用的页面/处理程序的上下文。

-西奥

System.Web.HttpContext.Current.Session

However, the context doesn't depend on the class, per say. It depends on the context of the page/handler that is using.

-Theo

樱娆 2024-09-02 05:17:34

我不知道这是什么类型的文件,但我对 .ashx 文件也有类似的问题。解决方案是处理程序必须实现 IReadOnlySessionState(用于只读访问)或 IRequiresSessionState(用于读写访问)。例如:

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

这些接口不需要任何额外的代码,而是充当框架的标记。

希望这有帮助。

乔纳森

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文