In ASP. NET, there are many types of objects that store information, such as application, session, Cookie, viewstate, and cache. What are their differences? What is the environment for each object application?
For a clearer understanding, we have summarized the specific environment of each object application, as shown in the following table:
Method |
Information size |
Save time |
Application Scope |
Save location |
Application |
Any size |
Entire application life cycle |
All users |
Server |
Session |
Small amount, simple data |
User Activity time + A delay period (average 20 minutes) |
Single User |
Server |
Cookie |
Small amount, simple data |
Can be set as needed |
Single User |
Client |
Viewstate |
Small amount, simple data |
Life cycle of a web page |
Single User |
Client |
Cache |
Any size |
Can be set as needed |
All users |
Server |
Hide domain |
Small amount, simple data |
Life cycle of a web page |
Single User |
Client |
Query string |
Small amount, simple data |
Until the next page Jump request |
Single User |
Client |
Web. config file |
A small amount of data that remains unchanged or rarely changed |
Until the configuration file is updated |
Single User |
Server |
1. Application Object
Application is used to save the public data information of all users. If the application object is used, a problem to be considered is that any write operation must be performed on the application_onstart event (Global. asax. although application. lock and applicaiton. the unlock method is used to avoid synchronization of write operations, but it serializes the requests to the application object. When the website traffic is large, it will produce serious performance bottlenecks. therefore, it is best not to use this object to store large data sets.
2. Session Object
Session is used to save the dedicated information of each user. Her survival period is the user's sustained request time plus a period of time (usually about 20 minutes). S
The information in ession is stored in the web server content, and the stored data volume can be large or small. the stored data is automatically released when the session times out or is disabled. because the user stops using the application and it remains in the memory for a period of time, the session object is used to save user data very efficiently. for a small amount of data, it is a good choice to use the session object to save. the code for saving information using the session object is as follows:
// Store information
Session ["username"] = "zhouhuan ";
// Read data
String username = session ["username"]. tostring ();
3. Cookie object
Cookie is used to save the request information of the client browser request Server Page. programmers can also use it to store non-sensitive user information. The time for saving the information can be set as needed. if no cookie expiration date is set, they are only saved until the browser program is closed. if the expires attribute of the cookie object is set to minvalue, the cookie will never expire. the amount of data stored in cookies is very limited. Most browsers support a maximum capacity of 4096. Therefore, do not store datasets and other large amounts of data. since not all browsers support cookies and data information is stored in plain text on the client's computer, it is best not to store sensitive, unencrypted data, otherwise, the website security will be affected. the code for saving the cookie object is as follows:
// Store information
Response. Cookies ["userid"]. value = "0001 ";
// Read information
String userid = response. Cookies ["userid"]. value;
4. viewstate object
Viewstate is often used to save the status information of a single user, and the validity period is equal to the lifetime of the page. The viewstate container can maintain a large amount of data, but it must be used with caution because excessive use affects the performance of applications. All Web server controls use viewstate to save their status information in the page sending and playback period. If a control does not need to save the status information during sending back, it is best to disable the viewstate of the object to avoid unnecessary resource waste. You can disable the viewstate of the entire page by adding the "enableviewstate = false" attribute to the @ page command. The code for saving information using the viewstate object is as follows.
// Store information
Viewstate ["nameid"] = "0001 ";
// Read information
String nameid = viewstate ["nameid"]. tostring ();
5. cache object
Cache objects are used to save pages or data between HTTP requests. This object can greatly improve the efficiency of the entire application. It allows you to store a large number of frequently accessed server resources in the memory. When a user sends the same request, the server returns the information stored in the cache to the user instead of processing it again, this reduces the time required for the server to process requests. The instance of this object is dedicated to each application, and its lifetime depends on the lifetime of the application. When the application is restarted, the instance of its cache object is re-created. The code for saving information using a cache object is as follows.
// Store information
Cache ["nameid"] = "0001 ";
// Store information
Cache. insert ("nameid", "0001" 1 );
// Read information
String nameid = cache ["nameid"]. tostring ();
6. Hide the domain
The hidden control is a server control of the HTML type. You can use this control to hide fields. In fact, this control is no different from other server controls, but it is not displayed in the browser of the user end and is always hidden. However, this control is submitted to the server together with other server controls each time the page is submitted. Therefore, the server can use the value attribute to obtain or save some data information. The following code uses the hidden control to save information.
// Store information
Hidden. value = "0001 ";
// Obtain information
String nameid = hidden. value;
7. query strings
To query a string, connect the value to be passed to the URL and use the response. Redirect method to redirect the client. In this way, information can be transmitted between two pages. Because the length of the URL is limited, too much information cannot be transmitted, and the security of the URL is not very good.
The transfer information is as follows.
Response. Redirect ("list. aspx? Nameid = 0001 & gradeid = 002 ");
// The code of the URL displayed in the IE Address Bar after executing the preceding statement is as follows.
Http: // localhost/list. aspx? Nameid = 0001 & grade = 002
// After you jump to list. aspx, you can use the following code to obtain the transmitted information.
String nameid. gradeid;
Nameid = request. Params ["nameid"];
Gradeid = request. Params ["gradeid"];
In addition to the objects described above, you can also use the context object and web. config configuration file.