简体   繁体   English

当前上下文中不存在“会话”。 我该如何使用它?

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

i got a problem in using Session.我在使用 Session 时遇到问题。 I was following many tutorials and documentations on different sites but still can't figure out how to use Session in my context.我在不同站点上关注了许多教程和文档,但仍然无法弄清楚如何在我的上下文中使用 Session。

So basically I want to use users login name during the whole session.所以基本上我想在整个 session 期间使用用户登录名。 I've read Session is the thing I need.我读过 Session 是我需要的东西。

Did i forget to import something?我忘记导入东西了吗? Am I using it wrong?我用错了吗?

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

Can someone please help me with that?有人可以帮我吗?

Thank you very much in advance for your answer.非常感谢您的回答。

For .NET Core try:对于 .NET 核心尝试:

using Microsoft.AspNetCore.Http;

then然后

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

For .NET framework (pre .NET 5) try:对于 .NET 框架(.NET 5 之前)尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM