Summary of ASP. NET saving information (Application, Session, Cookie, ViewState and Cache), cookieviewstate

Source: Internet
Author: User

Summary of ASP. NET saving information (Application, Session, Cookie, ViewState and Cache), cookieviewstate

The following is about ASP. comparison of objects storing various information in. NET, understanding the principles of these objects, is quite necessary for the creation of a complete program (abstract to the Internet, not original-xukunping)

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.


The object in aspnet that stores the read information is

In ASP. NET, there are many built-in objects for saving information, such as Application, Session, Cookie, ViewState and Cache.

What are the differences between application, session, cookies, and viewstate objects? (Discussed in terms of principle, scope of use, and access method)

Boss, it's too short to score 10 points. You need to write more words.

Application is used to save the data information shared by all users. If the stored data does not change or rarely change during the lifetime of the Application, use it. However, there is a web. config in asp.net, which may be better. To use the application, you must consider that any write operation must be completed in the application_onstart event (Global. asax. Although the application. lock and application. unlock methods are used to avoid Operation synchronization, They serialize application requests, which may cause performance bottlenecks when the Website access volume is large. Therefore, it is best not to use it to access large datasets.
Usage:
// Store information
Application ["test"] = "100 ";
// Read
String test = Application ["test"]. ToString ();

Session is used to save the dedicated information of each user. The lifetime of Session is the duration of the user's sustained request plus a period of time (which can be set in web. config. The default value is 20 minutes ). The Session information is stored in the server's memory. Of course, you can set the method for saving the Session information (such as the information stored in the SQL database ). Since the user stops using the program and it remains in the memory for a period of time, the efficiency of using the Session object to save user data is very low. For a small amount of data. Session is a good choice.
// Save
Session ["user"] = "majcms ";
// Obtain
String username = Session ["user"]. ToString ();

Cookie is used to save the request information of the client browser requesting the Server Page. programmers can also use it to save non-sensitive content. You can set the Save time as needed. If no Cookie expiration time is set, it will only be saved to the browser. If the Cookie is set to Min Value, it never expires. The Cookie storage capacity is limited. Generally, the maximum size of a browser is 4096 bytes. Therefore, it cannot be used to store large amounts of data. Since not all browsers support cookies and they are saved in plain text, it is best not to save sensitive content. Otherwise, network security is affected.
// Save
Response. Cookies ["name"]. Value = "majcms ";
// Obtain
String username = Response. Cookies ["name"]. Value;

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. Note: it is the lifetime of a single page, so its lifetime is smaller than the other three. The ViewState container can store a large amount of data, but it must be used with caution because excessive use may affect the performance. The most obvious difference is the slow speed of webpage opening. All Web server controls use ViewState to save their own information status during page sending back. If a control does not need to save information during sending back, it is best to disable its ViewState attribute, (check the property Panel) to avoid unnecessary resource waste. You can disable the ViewState of the entire page by setting "EnableViewState = false" on the page.
// Save
ViewState ["index"] = 1;
// Obtain
Int indexID = (int) ViewState [& ...... remaining full text>

Related Article

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.