ASP.NET Server Side Viewstate

I read several approaches for storing viewstate on the server:

Here is one

Here's another

But they are quite complicated. I am looking for a way to save an object without having to serialize it. I could use the session state, but if the user opens more than one window, there may be an overwrite of the object.

Is there a simple solution?

+3
source share
3 answers

In this situation, I would put the storage object into the session using a unique key and bind the key to the page. All this can be abstracted in the properties of the page class.

public string PersistanceKey
{
    get { 
        if(ViewState["PersistanceKey"] == null)
           ViewState["PersistanceKey"] = "Object" + Guid.NewGuid().ToString();

        return (string)ViewState["PersistanceKey"];
    }
}

public PersistanceObject Persistance
{
    get {
        if(Session[this.PersistanceKey] == null)
            Session[this.PersistanceKey] = new PersistanceObject();

        return (PersistanceObject)Session[this.PersistanceKey];
}

. Session ( Cache) , .

, . , .

+2

.

. , .

+2

Assign a number to each window that the user can open. Add this number to the session key. You must also save the number somewhere on the page (querystring or hidden input) to get the corresponding session variable.

+1
source

Source: https://habr.com/ru/post/1705041/


All Articles