Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static variable causing concurrency issue in mvc

We developing Web Application using mvc4. In many scenario we will get value from user in First Page/View which we need to keep in some varaiable until user reach Final Page/View. He/She may pass 4-5 views to reach final view from first view.

To keep Value in MVC. We had 3 ways.

1. Global Variable - But if i assign value in one action method. the value will be reset in another action method. So we dropped it.

2.Session - But we need to keep more then 5 values in each session. So we dropped it.

3.Static Varibale - Which works like charm. but in Multiple user it caused concurrency issue.

Is any other ways in mvc to keep values? please guide me.

like image 654
Ryder Avatar asked Dec 08 '25 19:12

Ryder


1 Answers

Static variables will persist for the life of application domain, that is why you are seeing the concurrency issues with multiple users.

See: Static Variables and their implications in ASP.Net websites

There shouldn't be any problem in storing five values in a session. You can have List<T> and store that in session. Like:

List<string> someValues = new List<string> {"A","B","C","D", "E",};
HttpContext.Current.Session["userValues"] = someValues;

To retrieve it:

var someValues =  HttpContext.Current.Session["userValues"] as List<string>;
if(someValues != null)
{
 // found
}

The only thing you should consider is the size of data. Sessions are stored at server level for each user, storing too much data could cause a problem, though it depends on your configuration.

You may also see: What is ViewData, ViewBag and TempData? – MVC options for passing data between current and subsequent request

like image 62
Habib Avatar answered Dec 10 '25 11:12

Habib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!