Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.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# 如何访问asp.net中另一个类文件的Global.asax文件中的session_Start中声明的会话_C#_Asp.net_Session_Global Asax_Httpcontext - Fatal编程技术网

C# 如何访问asp.net中另一个类文件的Global.asax文件中的session_Start中声明的会话

C# 如何访问asp.net中另一个类文件的Global.asax文件中的session_Start中声明的会话,c#,asp.net,session,global-asax,httpcontext,C#,Asp.net,Session,Global Asax,Httpcontext,这是用于写入会话变量的global.asax文件 ##Global.asax file ## 下面的类位于单独的库中,但位于相同的解决方案中 protected void Session_Start(object sender, EventArgs e){ Session["myName"] = "something"; } 但是这里的会话值为null。我无法填充字符串 请让我知道我做错了什么。您需要在会话启动方法中使用的类的方法或构造函数中传递会话值。同意@

这是用于写入会话变量的global.asax文件

##Global.asax file ##
下面的类位于单独的库中,但位于相同的解决方案中

protected void Session_Start(object sender, EventArgs e){
    Session["myName"] = "something";        
    }
但是这里的会话值为null。我无法填充字符串


请让我知道我做错了什么。

您需要在会话启动方法中使用的类的方法或构造函数中传递会话值。同意@sainpreep。你的班级不会自己意识到这一点。您必须按照sir的建议传递它的值。我建议您最好将会话值存储在类类型的对象中
    --------------------------------------------


    ## MyClass.cs ##

    using system.web;
    public class myclass{

        public string my method()
        {
            string x = httpcontext.current.Session["myName"];
            return x;
        }
    }