Warm tip: This article is reproduced from serverfault.com, please click

c#-“会话”在当前上下文中不存在。

(c# - 'Session' does not exist in the current context. How can I use it?)

发布于 2020-12-01 21:07:00

我在使用Session时遇到问题。我在不同站点上关注了许多教程和文档,但是仍然不知道如何在我的上下文中使用Session。

因此,基本上,我想在整个会话期间使用用户登录名。我已经读过Session是我所需要的。

我忘了进口东西吗?我使用错了吗?

https://i.stack.imgur.com/vwTMb.png

有人可以帮我吗?

预先非常感谢你的回答。

Questioner
dimauser
Viewed
0
dpberry178 2020-12-02 05:36:58

对于.NET Core,请尝试:

using Microsoft.AspNetCore.Http;

然后

HttpContext.Session.SetString("test key", "test value");

对于.NET Framework(.NET 5之前的版本),请尝试:

HttpContext context = HttpContext.Current;
context.Session["Test key"] = "Test value";