Asp. The difference between the net:application,session,cookie,viewstate and the cache

Source: Internet
Author: User
Tags server memory

In ASP. NET, there are many kinds of objects that hold information. For example: Application,session,cookie,viewstate and cache, so what's the difference? What is the environment in which each object is applied?

To get a clearer picture, we summarize the specific context in which each object is applied, as shown in the following table:

Method Information Size Save Time Application Scope Save Location
Application Any size Lifetime of the entire application All Users Server-side
Session Small, simple data User activity time + a delay time (typically 20 minutes) Individual users Server-side
Cookies Small, simple data Can be set as required Individual users Client
Viewstate Small, simple data The lifetime of a Web page Individual users Client
Cache Any size Can be set as required All Users Server-side
Hidden fields Small, simple data The lifetime of a Web page Individual users Client
Query string Small, simple data Until the next page jump request Individual users Client
Web. config file Constant or minimal change in small amounts of data Until the configuration file is updated Individual users Server-side

Application

1, application used to save all users sharing information;

2, in the ASP era, if the data to be saved is not or rarely changed during the lifetime of the application, then using 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 application to note that all writes are done in the Application_OnStart event (Global.asax), although you can use Application.Lock () to avoid conflicts, But it serialized the request to the application, which can produce the serious performance bottleneck;

4, do not use application to save large data volume information;

5. Code:

Write: application["UserID"] = "test";

READ: String UserName = application["UserID"]. ToString ();

Session

1, the session is used to save each user's proprietary information;

2, the lifetime of the session is the user continuous request time plus a period of time (generally 20 minutes or so);

3, session information is stored in the Web server memory, the amount of data saved can be small;

4, the session timeout or be closed will automatically release data information;

5, because the user stopped using the application after it still in memory for a period of time, so this method is less efficient;

6. Code:

Write: session["UserID"] = "test";

READ: String UserName = session["UserID"]. ToString ();

Cookies

1, the cookie is used to save the client browser Request Server page request information;

2, we can store non-sensitive user information, save time can be set as required;

3. If the cookie expiration date is not set, its life cycle is saved until the browser is closed;

4. The Expires property of the cookie object is set to MinValue to never expire;

5, the amount of data stored by the cookie is limited, most of the browser is 4K so do not store big data;

6. Since not all browsers support cookies, the data will be stored in plaintext in the client;

7. Code:

Write: resopnse.cookies["UserID"] = "test";

READ: String UserName = resopnse.cookies["UserID"]. ToString ();

ViewState

1, ViewState used to save the user's status information, the validity period is equal to the life cycle of the page;

2, can save a large amount of data but to use caution, because it will affect the performance of the program;

3, all Web server control is used viewstate in the page postback period to save the State;

4, do not need to close the @page inside set enableviewstate=false;

5. Code:

Write: viewstate["ID"] = "test";

READ: String id = viewstate["id"]. ToString ();

Cache

1. The cache is used to save pages or data during HTTP requests;

2, the use of the cache can greatly improve the efficiency of the entire application;

3, it allows to store frequently accessed server resources in memory, when the user makes the same request, the server is not processed again, but the data saved in the cache is returned directly to the user;

4, you can see that the cache saves time (server processing time);

5. The cache instance is proprietary to each application, its life cycle = = The application cycle, and the application restart will recreate its instance;

6, Note: If you want to use the cache cleanup, expiration management, dependencies and other functions must use the Insert or Add method method of adding information;

7. Code:

Write: cache["ID"] = "test"; or Cache.Insert ("ID", "Test");

READ: String id = cache["id"]. ToString ();

Hidden

1. The hidden control is a server control of the HTML type and is always in a hidden state;

2. It will be submitted to the server side with other server controls at each commit;

3. Code:

Write: Hidden.value = "King";

READ: String id = hidden.value; To use Runat=server

Query string

1, the way to query the string is to pass the value of the link behind the URL;

2, generally used to transfer information between pages;

3, because the length of the URL has a certain limit, so can not convey too much information;

4, security is not very good.

5. Code:

Value-Added page: Response.Redirect ("list.aspx?id=123&name=abc");

Value page: string name = request.querystring["Name"]; String id = request.querystring["id"];

Asp. The difference between the net:application,session,cookie,viewstate and the cache

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.