Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么可以';我不能读取会话变量吗_C#_Asp.net_Session - Fatal编程技术网

C# 为什么可以';我不能读取会话变量吗

C# 为什么可以';我不能读取会话变量吗,c#,asp.net,session,C#,Asp.net,Session,我有一个c#.net web应用程序。我创建会话变量,但当我离开创建会话变量的页面后尝试读取它们时,我无法读取 创建于第1页 Session["UserName"] = "WhatEver"; 那我会的 Response.Redirect("~/whatever.aspx"); 并尝试读取以读取新页面的Page_Load方法中的会话变量 string userName = Session["UserName"].ToString(); 我收到未设置为对象实例的对象引用。 我为什么会收到此

我有一个c#.net web应用程序。我创建会话变量,但当我离开创建会话变量的页面后尝试读取它们时,我无法读取

创建于第1页

Session["UserName"] = "WhatEver";
那我会的

Response.Redirect("~/whatever.aspx"); 
并尝试读取以读取新页面的Page_Load方法中的会话变量

string userName = Session["UserName"].ToString();
我收到未设置为对象实例的
对象引用。

我为什么会收到此错误?我可以做些什么来修复此问题

这可能有助于:

Response.Redirect("~/whatever.aspx",false);
发件人:

这不会中止线程,从而保留会话令牌。实际上,这个重载是由RedirectFromLoginPage在内部使用的


可能是
sessionState
在您的
config
文件中已关闭

<sessionState mode="Off/> 

我遇到了完全相同的问题,我成功地克服了这个问题,但坦率地说,我不完全理解解决方案,不管怎样,我所做的就是从“添加新项”菜单为我的项目创建一个“Global.asax”文件。只需将以下代码复制粘贴到全局文件中。此文件中的代码如下所示:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;

using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ENTER_YOUR_NAMESPACE
{
    public class Global : System.Web.HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {

        }

        void Application_End(object sender, EventArgs e)
        {
            /* Code that runs on application shutdown */
            Session_End(sender, e);
        }

        void Application_Error(object sender, EventArgs e)
        {

        }

        void Session_Start(object sender, EventArgs e)
        {

        }//end void Session_Start

        void Session_End(object sender, EventArgs e)
        {

        }//end void Session_End

    }//end class Global
}//end namespace

尝试此响应。重定向(“~/whater.aspx”,false);检查您的global.asax或第2页或母版页上附加的其他用户控件,您在其中使用了“会话.放弃”此链接解释了为什么response.redirect会导致会话值丢失-2800_或-do-it-right_2900.aspx是否确定包含
Session[“UserName”]=“where”的代码在第1页执行?@Krishna,你的链接断开了。我检查了web.config,我没有关闭“seesionState”。我还尝试添加到web.config。仍然不走运。@MartyGoetz在重定向之前,您能在同一页上检索会话值吗?请随意对上面的代码进行任何调整,并让我知道您是否知道它到底做了什么,因为正如我之前所说,我真的不完全理解解决方案的逻辑!!
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;

using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ENTER_YOUR_NAMESPACE
{
    public class Global : System.Web.HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {

        }

        void Application_End(object sender, EventArgs e)
        {
            /* Code that runs on application shutdown */
            Session_End(sender, e);
        }

        void Application_Error(object sender, EventArgs e)
        {

        }

        void Session_Start(object sender, EventArgs e)
        {

        }//end void Session_Start

        void Session_End(object sender, EventArgs e)
        {

        }//end void Session_End

    }//end class Global
}//end namespace