ASP. NET contains 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. However, it serializes the requests to the Application object. When the site traffic is large, it will produce a serious performance bottleneck. therefore, it is best not to use this object to store large data sets. the following is an example of online user statistics to illustrate this problem:
(Store the total website traffic in the form of files)
(1) Global. asax class
(2) WebForm1.aspx
Private Void Page_Load (object Sender, System. EventArgs E)
{
This. Label1.Text = "Number of users accessing the site :" + Application ["CurrentGuests"]. ToString ();
This. Label2.Text = "Total number of users who have visited the site :" + Application ["AllGuests"]. ToString ();
}