'Session' 在当前上下文中不存在。我该如何使用它?

'Session' does not exist in the current context. How can I use it?

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

所以基本上我想在整个会话期间使用用户登录名。我读过 Session 是我需要的东西。

我是不是忘记导入什么东西了?我用错了吗?

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

有人可以帮我吗?

非常感谢您的回答。

对于 .NET Core 尝试:

using Microsoft.AspNetCore.Http;

然后

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

对于 .NET 框架(.NET 5 之前)尝试:

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