我有一个 C# .net Web 应用程序。我创建了 session 变量,但是当我离开创建 session 变量的页面后尝试读取它们时,我无法读取它们。
创建于第 1 页
Session["UserName"] = "WhatEver";
然后我做
Response.Redirect("~/whatever.aspx");
并尝试在新页面的Page_Load方法中读取session变量
string userName = Session["UserName"].ToString();
我收到未设置对象实例的对象引用。
为什么我会收到此错误消息,我该如何解决该问题?
最佳答案
这可能有帮助:
Response.Redirect("~/whatever.aspx",false);
来自 this article :
This does not abort the thread and thus conserve the session token. Actually, this overload is used internally by RedirectFromLoginPage.
关于c# - 为什么我不能读取 session 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13102204/