Q
Q
Quber2015-02-17 12:46:05
ASP.NET
Quber, 2015-02-17 12:46:05

How to get stored value in ASP.NET MVC 4 session?

In one of the controller methods, I set the session value: In another controller method, I try to get the value: Null gets into the test variable. Tried HttpContext.Session same thing. Of course, the user first passes through the first method of the controller, then through the second method. If you get the value of the session in the first method of the controller, immediately after the recording, then the value is there. Works fine. But in the second method of the controller it seems to be absent.
Session["User.Project"] = "Привет";
string test = Session["User.Project"];

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2015-02-17
@geeek

var context = HttpContext.Current

context.Session["user_id"] = id;

var id = (int) context.Session["user_id"];

A
Andrey Grishin, 2015-05-25
@lumini

string test = Session["User.Project"];
It won't compile? Session returns an object, not a string.
We need to check the code for Session.Clear() or any other session state changes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question