Application, Session, Cookie, Viewstate, and Cache object usage and Difference

Source: Internet
Author: User

========================================================== ==========================================================

1. Application: Used to save 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 ";
// Read information
String NameID = Application ["UserNameID"]. ToString ();

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: 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 the application. All Web server controls use ViewStat to save their own 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 the following:
// 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 strings: Connect the passed value to the end of the URL and use the Response. Redirect method to Redirect the client.

Use the following:
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. The Application is used to save information shared by all users.

2. In the Asp era, if the data to be stored does not or rarely change during the lifetime of the Application, Application is the ideal choice. 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 Web server memory, 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 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. Cookie is used to save the request information of the client browser requesting 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 amount of data stored in cookies is limited. Most browsers use 4 K, so do not store magnified 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 the user sends the same request
Instead of processing the data again, the server directly returns the data saved in the Cache to the user.

4. We can see that Cache saves time-server processing time

5. the Cache instance is exclusive to every application, and its lifecycle = This 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.