简体   繁体   中英

Maintain state of the object for same browser session

Following is my ASP.Net Web API controller code. Here as you can see there's a private class object, BL, that is used and both the Get methods implemented. For the first method FetchAllDashboardByUserId(int userId), I pass the user id so that the BL object can be initiated. Within the same browser session, if the second get method is called, then I do not want to pass the userid, since BL should be by default initiated, but currently that's not the case. BL is null for the second method, so I have to add userid to the call to the method - GetCardDataUI(int userId, int dashBoardID, int cardID). My question is how to avoid it. Is my thinking incorrect that:

  • A single open browser where I make the Consecutive call to the following URLs are a single session:

    webapi/ViewR?userId=1

    webapi/ViewR?userId=1&dashBoardID=1&cardID=3

I don't want to pass the userId in the second URL. Please note that if I declare class object as static then it works as expected, but that's not what I want, it has to be tied to a user:

public class ViewRController : ApiController
    {
        // BL object for a user
        private static BL accessBL = null;

        // HTTP GET for Webapi/ViewR (Webapi - name of API, ViewR  - Controller with implementation)            

        [AcceptVerbs("Get")]
        public List<DashboardUI> FetchAllDashboardByUserId(int userId)
        {
            if (accessBL == null)
                accessBL = new BL(userId);

            // Use BL object for entity processing
        }

        [AcceptVerbs("Get")]
        public CardDataGetUI GetCardDataUI(int userId, int dashBoardID, int cardID)
        {
            if (accessBL == null)
                accessBL = new BL(userId);

            // Use BL object for entity processing
        }
    }

How I want the second method implementation to be:

[AcceptVerbs("Get")]
            public CardDataGetUI GetCardDataUI(int dashBoardID, int cardID)
            {
               // Use BL class object created in last call for entity processing
               // Should not pass userid again
            }

You can easily store data in Session :

... first request:

Session["userID"] = userID;

... next request:

int userID = (int)Session["userID"];  // should check for null first, but you get the idea...

But keep the following points in mind:

  • Session variables are stored as object s, so you'll need to cast and/or type-check
  • Session variables can be null
  • Session expires after a configurable (in web.config ) about of time
  • Default session state is in-memory, meaning if the app pool is restarted session state is gone - you can store session in files or databases to keep longer
  • Session doesn't scale out unless you use persistent storage (file, database)
  • Objects stored in persistent storage must be serializable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM