为什么在asp.net-mvc项目中,我的会话变量在Controller构建阶段始终无法正常访问?
- 内容介绍
- 文章标签
- 相关推荐
本文共计244个文字,预计阅读时间需要1分钟。
在尝试从构造函数中获取存储在Session中的信息时,使用ASP.NET MVC时发现Session尚未设置。但一旦构建了控制器,Session就会包含正确的信息。以下是修改后的代码示例:
csharppublic class ABCController : Controller{ public ABCController() { // 构造函数中尝试从Session获取信息 if (Session[objectName]==null) { // 如果Session中无信息,则进行初始化 } else { // 从Session中获取信息 } }}
在尝试从构造函数中获取存储在Session [“objectName”]上的信息时使用ASP.NET MVC,我看到Session尚未设置,但是一旦构造了控制器,Session就会包含正确的信息.public class ABCController : Controller { public ABCController() { var tmp = Session["Whatever"]; } //This line is null //But I know it has information public ActionResult Index() { var tmp = Session["Whatever"]; } //This works fine }
谢谢
本文共计244个文字,预计阅读时间需要1分钟。
在尝试从构造函数中获取存储在Session中的信息时,使用ASP.NET MVC时发现Session尚未设置。但一旦构建了控制器,Session就会包含正确的信息。以下是修改后的代码示例:
csharppublic class ABCController : Controller{ public ABCController() { // 构造函数中尝试从Session获取信息 if (Session[objectName]==null) { // 如果Session中无信息,则进行初始化 } else { // 从Session中获取信息 } }}
在尝试从构造函数中获取存储在Session [“objectName”]上的信息时使用ASP.NET MVC,我看到Session尚未设置,但是一旦构造了控制器,Session就会包含正确的信息.public class ABCController : Controller { public ABCController() { var tmp = Session["Whatever"]; } //This line is null //But I know it has information public ActionResult Index() { var tmp = Session["Whatever"]; } //This works fine }
谢谢

