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

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

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

i got a problem in using Session. I was following many tutorials and documentations on different sites but still can't figure out how to use Session in my context.

So basically I want to use users login name during the whole session. I've read Session is the thing I need.

Did i forget to import something? Am I using it wrong?

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

Can someone please help me with that?

Thank you very much in advance for your answer.

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

For .NET Core try:

using Microsoft.AspNetCore.Http;

then

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

For .NET framework (pre .NET 5) try:

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