Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net session variable

I have a asp.net project with c# code behind. I have a static class called GlobalVariable where I store some information, like the currently selected product for example.

However, I saw that when there are two users using the website, if one changes the selected product, if changes it for everybody. The static variables seem to be commun to everybody.

I would want to create (from the c# code) some kind of session variable used only from the c# code, but not only from the page, but from any class.

like image 627
Amaranth Avatar asked Mar 08 '12 18:03

Amaranth


People also ask

What is a session variable in C#?

The Session object stores information about, or change settings for a user session. Variables are stored in a Session object hold information about one single user. And are available to all pages in one application. Common information stored in session variables are name, id, and preferences.

How can use session variable in ASP.NET MVC?

Sessions values can be stored for the duration of the visitor's session on your site. Most cases, they are stored in server memory. You can configure session to store either in State server or in SQL Server. In ASP.NET MVC, you can create and access session variables using HttpContext.


1 Answers

Yes static variables are shared by the whole application, they are in no way private to the user/session.

To access the Session object from a non-page class, you should use HttpContext.Current.Session.

like image 180
driis Avatar answered Oct 09 '22 17:10

driis