Use objects such as Application, Session, Cookie, and ViewState

Source: Internet
Author: User

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 ();
                }

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.