Application Session Cookie viewstate cache hidden difference

Source: Internet
Author: User

ASP. NET contains a variety of objects that store information, such as application, session, Cookie, viewstate, and cache. What are their differences? What is the application environment?

For a clearer understanding, we have summarized the specific environment of each object application, as shown in the following table:



Application

1. application is used to saveShared by all usersInformation;

2. In the ASP era, if the data to be stored is appliedProgramApplication is an ideal choice if no or few changes occur during the lifetime. However, in the Asp.net development environment, we put similar configuration data in Web. config;

3. If you want to use the application, note that all write operations must be completed in the application_onstart event (Global. asax), even though application. lock () avoids conflicts, but serializes application requests, resulting in severe performance bottlenecks;

4. Do not use application to save large data volumes;

5,Code:

application [ " userid " ] = " test " ; /// write

StringUsername = application ["Userid"]. Tostring ();// Read 


session
1. Session is used to save private information of each user ;
2. The session lifetime is the duration of the user's sustained request plus a period of time (generally about 20 minutes);
3. The session information is stored in the memory of the Web server. The data size can be large or small.
4. Data is automatically released when the session times out or is disabled.
5. This method is inefficient because it remains in the memory for a period of time after the user stops using the application.
6. Code:

session [ " userid " ] = " test " ; /// write

StringUsername = session ["Userid"]. Tostring ();// Read 


cookie
1. Cookies are used to save client browser request Server Page request information;
2. We can store non-sensitive user information, you can set the Save time as needed.
3. If no cookie expiration date is set, the lifecycle of the cookie is saved until the browser is closed;
4. Setting the expires attribute of the cookie object to minvalue indicates that the object will never expire.
5. The volume of data stored in cookies is limited. Most browsers use 4 K. Therefore, do not store enlarged data.
6. Because not all browsers support cookies, data is stored on the client in plain text.
7. Code:

resopnse. cookies [ " userid " ] = " test " ; /// write

StringUsername = resopnse. Cookies ["Userid"]. Tostring ();// Read 


viewstate
1. viewstate is used to save User status information, the validity period is equal to the lifecycle of the page.
2. You can save a large amount of data, but use it with caution because it affects program performance;
3. All Web server controls use viewstate to save the status during page PostBack.
4. Disable enableviewstate = false in @ page if not required;
5. Code:

viewstate [ " ID " ] = " test " ; /// write

StringId = viewstate ["ID"]. Tostring ();// Read 

 

cache
1. cache is used for During the HTTP request period , save the page or data.
2. The use of cache can greatly improve the efficiency of the entire application.
3. It allows storing frequently accessed server resources in the memory. When the user sends the same request, the server directly returns the data stored in the cache to the user instead of processing it again.
4. The cache saves time (server processing time).
5. the cache instance is exclusive to each application. its lifecycle = This application cycle. When the application restarts, it will be re-created;
6. Note: if you want to use functions such as cache cleanup, expiration management, and dependency items, you must use the insert or add method to add information.
7. Code:

cache [ " ID " ] = " test " ; or cache. insert ( " ID " , " test " ); /// write

StringId = cache ["ID"]. Tostring ();// Read 

 

hidden
1. The hidden control is an HTML-type server control. To use runat = server, always hidden ;
2. Each time it is submitted, it will be submitted to the server with other server controls;
3. Code:

hidden. value = " King " ; /// write

StringId = hidden. value;// Read 

 

query string
1. the query string is connected to the URL by the value to be passed;
2. It is generally used to transmit information between pages;
3. The URL length is limited, so too much information cannot be transmitted.
4. Poor security.
5. Code:

response. redirect ( "list. aspx? Id = 123 & name = ABC " ); // value transfer page

StringName = request. querystring ["Name"];// Value page

StringId = request. querystring ["ID"];

 

Global. asax File

Application_start:

This method is triggered by the first Website user. this method usually defines some system variables, such as the total number of online users in the chat room, and the initialization of historical access statistics.

Application_end:

This method is triggered when the website is closed or restarted. Too many tests are not performed.

Session_start:

Triggered when each user accesses the first page of the website;

Session_end:

If session. Abandon () is used, or the session times out, it can be triggered after the user exits.

Note:

The Session object, server. mappath (), and server object cannot be used in session_end. Therefore, only session. Contents ("username") can be used to replace session ("username"), or application objects or methods;

For example:

You want to use server in session_end. mappath ("users. application ("xmlpath") = server. mappath ("users. XML "), and then call application (" xmlpath ") in session_end, instead of directly using server in session_end. mappath ("users. XML "),

 

Application and session execution sequence:

1. application_start

2. session_start

3. session_end

4. application_end

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.