gpt4 book ai didi

c# - 从 System.Web.UI.Page 继承时 Session 为空

转载 作者:行者123 更新时间:2023-11-30 15:10:39 24 4
gpt4 key购买 nike

我想用一些额外的东西扩展 System.Web.UI.Page 类。在 ctor 中,我需要 session 变量的值。

问题是 session 对象为空...

public class ExtendedPage : System.Web.UI.Page {
protected foo;

public ExtendedPage() {
this.foo = (int)HttpContext.Current.Session["foo"]; // NullReferenceException
}
}

如果我将带有 session 对象的部分移动到加载事件中,一切正常......

public class ExtendedPage : System.Web.UI.Page {
protected foo;

public ExtendedPage() {
this.Load += new EventHandler(ExtendedPage_Load);
}

void ExtendedPage_Load(object sender, EventArgs e) {
this.foo = (int)HttpContext.Current.Session["foo"];
}
}

为什么在第一种情况下 session 对象null??

最佳答案

Session 属性稍后在 Page lifecycle 中设置, Page 对象构建完成后。

关于c# - 从 System.Web.UI.Page 继承时 Session 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3054229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com