Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.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
Asp.NET C#从类到处理程序的会话变量_C#_Asp.net_Class_Session_Handler - Fatal编程技术网

Asp.NET C#从类到处理程序的会话变量

Asp.NET C#从类到处理程序的会话变量,c#,asp.net,class,session,handler,C#,Asp.net,Class,Session,Handler,为了避免使用querystring,我正在尝试使用会话变量来传递所需的值。 我是从一个类到一个处理程序 在我使用的课堂中: HttpContext.Session["name"] = "Value"; 在我使用的处理程序中: var name = context.Session["name"]; 我已经添加了IReadOnlySessionState或irerequiresessionstate,但会话变量仍然为空 有什么帮助吗?我想你又在创造新的环境了。您应该使用HttpContext.C

为了避免使用querystring,我正在尝试使用会话变量来传递所需的值。 我是从一个类到一个处理程序

在我使用的课堂中:

HttpContext.Session["name"] = "Value";
在我使用的处理程序中:

var name = context.Session["name"];
我已经添加了
IReadOnlySessionState
irerequiresessionstate
,但会话变量仍然为空


有什么帮助吗?

我想你又在创造新的环境了。您应该使用
HttpContext.Current
使用相同的会话。 尝试下面的代码,并参考我在下面添加的注释

    string firstName = "Jeff";
    string lastName = "Smith";
    string city = "Seattle";

    // Save to session state in a Web Forms page class.
    Session["FirstName"] = firstName;
    Session["LastName"] = lastName;
    Session["City"] = city;

    // Read from session state in a Web Forms page class.
    firstName = (string)(Session["FirstName"]);
    lastName = (string)(Session["LastName"]);
    city = (string)(Session["City"]);

    // Outside of Web Forms page class, use HttpContext.Current.
    HttpContext context = HttpContext.Current;
    context.Session["FirstName"] = firstName;
    firstName = (string)(context.Session["FirstName"]);

你好谢谢你的回答。。。但它不起作用。。。我正在设置类中的值,但在处理程序上,它仍然为null。