Generally, HTTP is a non-State service that does not store any user request information. Until Netscape began to use cookies to store user request information, the Web application had a great development. Currently, ASP. NET programmers can save user request information in many ways.
The following describes several methods of Status Management in ASP. NET.
Client
ASP. NET provides cookies, querystrings (URL), hidden fields, view State and control state (ASP. NET 2.0) to manage client requests.
I. Cookie
Cookie is stored in the computer as text and matched with name-value. The user stores the user information, such as the user name and user configuration. Although cookies can be encrypted and stored, it is not recommended to store user passwords in cookies. Second, the cookie size is limited, so it is not suitable for storing large data.
Example:
1. Get user ID
If (request. Cookies ["userid"]! = NULL)
Lbmessage. Text = "dear" + request. Cookies ["userid"]. Value + ", welcome to our website! ";
Else
Lbmessage. Text = "guest, welcome to our website! ";
2. Set cookie
Response. Cookies ["userid"]. value = username;
Advantages:
Easy to use
Disadvantages:
1. The client can disable cookies.
2. Cookie is loaded every time a request or sending, affecting transmission.
3. It is easy to crack and is not suitable for storing security information.
II. Hidden fields (hidden Control)
Hidden field stores information at the page level. The difference from other user-standard controls is that the hidden field is not displayed on the page. When the page is submitted, the value in the hidden field will be sent to the server in the same way. Although viewstate can be used in ASP. NET 2.0, hidden fields can still be used to store non-critical information.
Protected system. Web. UI. htmlcontrols. htmlinputhidden hidden1;
// To assign a value to hidden field
Hidden1.value = "create hidden fields ";
// To retrieve a value
String STR = hidden1.value;
Advantages:
1. Easy to use
2. Small amount of data can be stored
Disadvantages:
1. Insecure, transmitted directly from the network in plaintext
3. View state)
Status view viewsate, which can be used to store information for individual users. You can set the enableviewstate attribute to control the use of viewstate. The default value is true. Viewsate is transmitted every time a user request or server response occurs. Therefore, to reduce network bandwidth usage, you can set the value of enableviewstate to false when you do not need a status view. The storage of viewsate in the page is base64 transcoded, and the additional data volume is added. Therefore, you must note that you save a small number of viewsate files.
// Add item to viewstate
Viewstate ["myviewstate"] = myvalue;
// Reading items from viewstate
Response. Write (viewstate ["myviewstate"]);
Advantages:
Easy to operate
Encrypted
It can be used at the control level
Disadvantages:
Encrypted encoding increases the page size.
Increased price for Network Transmission
Iv. query strings
Query string is often used for page values. Many browsers have a length limit of 255 bytes. It is worth noting that try to use server. urlencode to avoid some unexpected situations, such as SQL injection.
String productid;
Productid = request. Params ["productid"]
Server
I. Application
The application is visible throughout the application, and all users share the same application. In ASP, it is often used to store link strings.
Application. Lock ();
Application ["mydata"] = "mydata ";
Application. Unlock ();
II. Session
Session stores user information separately for each user. Session can be stored in three ways. "Inproc", "sqlserver", "StateServer.
Inproc, data is stored in the server process. Due to the current memory limit of MB, the process can be restarted, and all the status information is lost.
Sqlserver, stored in the database, and the user status can be permanently retained.
StateServer, which is stored in another status server.
Session ["userid"] = "userid ";
String userid = session ["userid"]. tostring ();