I'm adding some new functionality to an MVC4 site. I have just added a global static class and a global variable to hold a value in which is determined at some point during a users visit to the site and will be used as a condition later on.
The thought then occurred to me that I was unsure of just how global a static value is in this context.
Do I have to specify the global value as a session variable?
Or will each user to the site get access to it in their own session anyway and I don't have to worry about one user setting it to one value and another user having access to this set value.
Your basic question: Are static globals shared among all users?
Because each MVC site is an application, all statics are shared among your app's user pool.
Deeper Question: How do I store user data for the user's entire stay?
Your question about statics will earn you sneers from some and appreciation from others. Sneers because you could look this up, appreciation because your underlying question on storing user data for easy retrieval is an important one most of us have faced.
You already used a session. Sessions are awkward. They expire. They're particularly unreliable when you use load balancing. To deal with expired sessions, I've used action filters; The action filter checks for null session variable OnActionExecuting
, re-populating the session if it is NULL
.
I've had more success with cookies. Cookies have their own issues. Some client browsers don't support them. But cookies are accepted more and more often. The objections become less common.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With