Application, session, Cookie, viewstate, cache object usage

Source: Internet
Author: User

ASP. NET, a built-in object for saving information, suchApplication,Session,Cookie,ViewstateAndCacheAnd so on

Built-in object method information size scope and storage time range
========================================================== ============================================
Application Data of any size throughout the life cycle of the application entire application/All Users
Session a small amount of simple data user activity time + Delay Time (20 minutes by default) for a single user
COOKIE: a small amount of simple data can be set for a single user as needed
Viewstate a small amount of simple data a Web page life cycle a single user
You can set the entire application/all users as needed
Hiding a small amount of simple data in a domain a Web page life cycle for a single user
Query a small amount of simple string data until the next page jumps to the request service for a single user

========================================================== ========================================================== ==========
1. application:Stores data shared by all users. Similar configuration data in ASP. NET is best stored in the web. config file. If the application object is used, a problem to be considered is that any write operation must be completed in application_onstart event (Global. asax. Although the application. Lock and application. Unlock methods are used to avoid synchronization of write operations, the method serializes Application Object Requests. When a website has a large traffic volume, a severe performance bottleneck occurs. Therefore, it is best not to use this object to save large datasets.

Use:
// Store information
Application ["usernameid"] = "1000 ";

Application. Add ("user", datatable );
// Read information
String nameid = application ["usernameid"]. tostring ();

Gridview1.datasource = application ["user"] As datatable;
Gridview1.databind ();

2. Session:Used to save the private information of each user. The session information is stored in the memory of the Web server, and the stored data volume can be large or small. The stored data is automatically released when the session times out or is disabled. It is a good choice to save a small amount of data session objects.

Use:
// Store information
Session ["usernameid"] = "1000 ";
// Read information
String nameid = session ["usernameid"]. tostring ();

3. COOKIE:It is used to save the request information of the client browser request server page. The validity period can be set manually, and the data size stored is very limited. Therefore, do not save datasets and other large amounts of data. Moreover, cookies store data in plain text on the client's computer, so it is best not to store sensitive unencrypted data.

Use:
// Store information
Response. Cookie ["usernameid"]. value = "1000 ";
// Read information
String nameid = response. Cookie ["usernameid"]. value;

4. viewstate:It is often used to save the status information of a single user. It can save a large amount of data, but excessive use may affect the performance of applications. All Web server controls use viewstat to save their status information during page sending. Each control has its own viewstate. It is best to disable it when not in use to save resources. You can disable the viewstate of the entire page by adding the "enableviewstate = false" attribute to the @ page command.

Use:
// Access information
Viewstate ["usernameid"] = "1000 ";
// Read information
String nameid = viewstate ["usernameid"]. tostring ();

5. cache:Used to save pages and data between HTTP requests. 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 server's request processing time.

Use:
// Store information
Cache ["usernameid"] = "1000 ";
// Store information
Cache. insert ("usernameid", "1000 ");
// Read information
String usernameid = cache ["usernameid"]. tostring ();

6. Hide the domain:The hidden control is an HTML-type server control that can hide a domain. It is no different from other spaces except that it is not displayed in a browser and is always hidden.

Use:
// Store information
Hidden. value = "1000 ";
// Read information
String usernameid = hidden. value;

7. query string:Connect the passed value to the end of the URL and use the response. Redirect method to redirect the client.

Use:
Response. Redirect ("user. aspx? Usernameid = 10000 & leveld = 100 ");
The code of the URL displayed in the IE Address Bar after executing the preceding statement is as follows:
Http: // localhost/user. aspx? Usernameid = 1000 & levelid = 100
After redirecting to user. aspx, you can use the following code to obtain the transmitted information:
String usernameid, leveld;
Usernameid = request. Params ["usernameid"];
Leveld = request ["leveld"];

========================================================== ========================================================== ==========
Application
1. application is used to save information shared by all users.
2. In the ASP era, it is ideal to use application if the data to be stored does not or rarely change during the lifetime of the application. 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, which can cause serious performance bottlenecks.
4. Do not use application to save large data volumes.
5. Code: application ["userid"] = "test ";
String username = application ["userid"]. tostring ();

Session
1. Session is used to save the proprietary information of each user.
2. The session lifetime is the user's sustained request time plus a period of time (generally about 20 minutes)
3. The session information is stored in the memory of the Web server, and the stored data volume can be large or small.
4. Data will be 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 ";
String username = session ["userid"]. tostring ();

Cookie
1. Cookies are used to save the request information sent from the client browser to the server page.
2. We can store non-sensitive user information and set the storage time as needed.
3. If no cookie expiration date is set, its lifecycle 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, so do not store enlarged data.
6. Because not all browsers support cookies, data will be stored in plaintext on the client.
7. Code: resopnse. Cookies ["userid"] = "test ";
String username = resopnse. Cookies ["userid"]. tostring ();

Viewstate
1. viewstate is used to save the user's 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 will affect 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"] = "CC ";
String id = viewstate ["ID"]. tostring ();

Cache
1. cache is used to save pages or data during HTTP requests
2. The use of cache can greatly improve the efficiency of the entire application.
3. It allows you to store frequently accessed server resources in the memory. When a user sends the same request, the server directly returns the data stored in the cache instead of processing it again.
4. We can see that cache saves time-server processing time
5. the cache instance is exclusive to every application, and its lifecycle = The application cycle
Restart the application and re-create its instance
6. Note: 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"] = "cc"; or cache. insert ("ID", "test ");
String id = cache ["ID"]. tostring ();

Hidden
1. The hidden control is an HTML-type server control and is always hidden.
2. Each time it is submitted, it will be submitted to the server together with other server controls.
3. The Code is as follows: hidden. value = "CC ";
String id = hidden. value; Use runat = Server

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.