Classes are serialized and stored in COOKIES.

Source: Internet
Author: User
We know that there are many ways to pass values between pages in. NET, such as SESSION, COOKIES, Response. Redirect, URL, and so on;
Which method is used to analyze specific issues? For example, if a field needs to be uploaded to the next page, the URL and Response. Redirect methods are the best choice;
If the object is large, we will consider using session, cookies, and sessions. If the data volume is large, IIS may crash and site access will fail,
Then we will use COOKIES. Some friends will say that cookies are easy to lose. Indeed, if cookies are used well, they will not be lost.
Next, we will introduce how to serialize class objects and save them in COOKIES.
Before using serialization, let's first think about how to serialize the object and then write it into COOKIES? Will it not work without serialization? What will the object look like after serialization?
Let's take a look at the source code with these questions:
First, create a class. [Serializable] indicates that the class can be serialized.

[Serializable]
Public class mbmembers
{
Public mbmembers ()
{

}

# Region "Private Variables"
Private string _ M_UserName; // account
Private string _ M_RealName; // real name
Private string _ M_IDCard; // ID card number
Private string _ M_Email; // email address
Private string _ M_Mobile; // mobile phone number
# Endregion

# Region "Public Variables"
/// <Summary>
/// Account
/// </Summary>
Public string M_UserName
{
Set {this. _ M_UserName = value ;}
Get {return this. _ M_UserName ;}
}

/// <Summary>
/// Real name
/// </Summary>
Public string M_RealName
{
Set {this. _ M_RealName = value ;}
Get {return this. _ M_RealName ;}
}

/// <Summary>
/// ID number
/// </Summary>
Public string M_IDCard
{
Set {this. _ M_IDCard = value ;}
Get {return this. _ M_IDCard ;}
}

/// <Summary>
/// Email address
/// </Summary>
Public string M_Email
{
Set {this. _ M_Email = value ;}
Get {return this. _ M_Email ;}
}

/// <Summary>
/// Mobile phone number
/// </Summary>
Public string M_Mobile
{
Set {this. _ M_Mobile = value ;}
Get {return this. _ M_Mobile ;}
}
# Endregion
}
The following describes the serialization and deserialization methods:

/// <Summary>
/// Binary serialization of class mbmembers into a string
/// </Summary>
/// <Returns> serialization Code </returns>
Public string BinarySerialize (mbmembers model)
{
BinaryFormatter ser = new BinaryFormatter ();
MemoryStream mStream = new MemoryStream ();
Ser. Serialize (mStream, model );
Byte [] buf = mStream. ToArray ();
MStream. Close ();

Return System. Text. Encoding. Default. GetString (buf );
}
/// <Summary>
/// Deserialize the string retrieved from the Cookie into an mbmembers class
/// </Summary>
/// <Param name = "binary"> </param>
/// <Returns> </returns>
Public mbmembers DeSerialize (string strLoginUserModel)
{
Byte [] binary = System. Text. Encoding. Default. GetBytes (strLoginUserModel );

BinaryFormatter ser = new BinaryFormatter ();
MemoryStream mStream = new MemoryStream (binary );
Mbmembers o = (mbmembers) ser. Deserialize (mStream );
MStream. Close ();
Return o;
}

Serialize user personal information and write it into COOKIES:
Mbmembers bs = new mbmembers ();

Response. Cookies ["userinfo"]. Value = BinarySerialize (bs); // serialization
Response. Cookies ["userinfo"]. Expires = DateTime. MinValue;


String str = Request. Cookies ["userinfo"]. Value; // deserialization
Mbmembers mb = DeSerialize (str );

The above are all source code, which may be difficult to understand. First of all, you should understand the purpose of serialization. The purpose of serialization is to transmit objects over the network. Otherwise, object-oriented distributed computing cannot be implemented. For example, your client needs to call a method on the server to obtain a Product object. For example, the method is public Product findProduct (int product_id );
Note that this method returns a Product object. Without serialization technology, the client will not receive the returned Product object. The serialization is to convert an object into a byte stream that can be transmitted over the network.

However, if you do not want to serialize the object, it is also possible to directly store the object in COOKIES. In this case, you need to traverse the information in the object cyclically and splice the information into a string, and then stored in COOKIES.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.