Link to home
Start Free TrialLog in
Avatar of Eddie Shipman
Eddie ShipmanFlag for United States of America

asked on

"Static" data per user in ASP.Net MVC application

I created a "singleton" object in a base controller and it is used for several parts in my Home controller actions.
private Participant _Part;
public Participant Part
{
    get 
    {
        if(_Part == null) {
            // LT and PID are stored in Session variables when user logs in
            _Part = mps.GetPart(LT, PID);
        }
        return _Part;
    }
    set
    {
        _Part = value;
    }
}

Open in new window

Used in Home Controller:
public ActionResult MyInfo()
{
    ViewBag.part = Part;
    return view();
}

Open in new window

An in my View:

    <input type="hidden" name="Desig" id="Desig" value="@ViewBag.part.Desig" />
    <input type="hidden" name="Discount" id="Discount" value="@ViewBag.part.Discount" />


Now, If I login as one person, the session variables are set.
Then I open another browser and login as a different user, I see the same data in the other browser.

How do I prevent this and keep the data 'static" for each user?

Do I need to store it in a session variable? is it capable of being store in a session variable?
Avatar of Najam Uddin
Najam Uddin
Flag of United States of America image

Well, looking at code it should not behave like static. Are you sure you didn't use static keyword in class, ie your class definition is as is you pasted above? As every user request will have separate instance.
Avatar of Eddie Shipman

ASKER

Well, it acts more like a singleton than a static. However it seems that it is shared for the entire app vs. between sessions.

I saw behavior where a user logging in saw the same data as someone that was already logged in so I do not believe each request has a different instance.

I rewrote it to store to session but it also shares between tabs in same browser. It does work for different browsers, however:
private Participant _Part;
public Participant Part
{
    get
    {
        System.Web.HttpSessionStateBase Sess = HttpContext.Session;
        if ((Participant)Sess["PartData"] == null)
        {
            // LT and PID are stored in Session variables when user logs in
            _Part = mps.GetPart(LT, PID);
            HttpContext.Session.Add("PartData", _Part);
        }
        else
        {
            _Part = (Participant)Sess["PartData"];
        }
        return _Part;
    }
    set
    {
        _Part = value;
    }
}

Open in new window

Between tabs, I can understand as they share same session, until you explicitly open a tab as new session.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Has anyone seen methods of caching data like this for ASP.Net MVC applications? I need to cache PER USER, not necessarily per tab.
Why would two users be on the same machine?
Our phone operators may need to open 2 user accounts on same machine.
Point taken. I'll have to bow out, then. I believe there are some hacky ways to accomplish this, but I haven't personally done any of them. You're basically fighting the browser at this point.
Do you think something like CacheCow would work for this?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Najim, How will that work in ASP.Net MVC?
Wont work in MVC, newer tech from microsoft don't support cookie less.

 However there is another way, but not 100% sure

1. Create a random identifier and store in hidden field

//in pageload
  if(!IsPostback)
   //generate random unique identifier

and store session key with this identifier

Session(uniqueIdentifier + session key) = value.

Open in new window


It will be different for each page rendered, so different tab will have actually different values from session
Also if you are using HTML5 then sessionStorage will help

Data stored using sessionStorage do not persist across browser tabs, even if two tabs both contain webpages from the same domain origin. In other words, data inside sessionStorage is confined to not just the domain and directory of the invoking page, but the browser tab in which the page is contained in. Contrast that to session cookies, which do persist data from tab to tab.
@Najam

I believe you'd lose a user's session--effectively--if they navigated directly to a page (via the address bar) using that approach (in http:#a41241266). They'd still have an open session, but the unique ID would be different, so you wouldn't be able to get the original values.
@käµfm³d 👽
If request is send back to server with page id (unique identifier) they should be getting saved value. Yes you are right that getting this to work in other pages will be complicated.

I am trying to think of any kind of tweak that can work, Like for sessionStorage, this can be base to such solution. One can generate a unique id, store in sessionStorage and other tab will not have this value so it will generate its own unique id. Session will be same for tabs, but by appending unique id we can different value set for different tabs
@Najam
LocalStorage won't help, either because we have many IE users with versions < IE10.
Besides, one other thing we need, a list of documents, may be thousands of records.
Then only option I see is playing around previous solution, although as pointed by @käµfm³d 👽 this will be  complex thing to do
May have to go another route to get this solved.