Application/session/Cookie/viewstate/Cache/hide domain/query string comparison

Source: Internet
Author: User



Application

1. Application is used to save information shared by all users. When IIS is stopped, it is initialized.
2.In the ASP era, it is ideal to use application if the data to be stored does not or rarely change during the lifetime of the application. 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. When a website has a large volume of traffic, it will produce a severe performance bottleneck. Global. asax needs to be established, and pessimistic lock/unlock is required.
4. Do not use application to save large data volumes
5.Code:
Global. asax

Void Application_start ( Object Sender, eventargs E)
{
Application [ " A " ] =   0 ;
}

. CS

...()
{
Application. Lock ();
Application [ " A " ] = ( Int ) Application [ " A " ] + 1 ;
Sting=Application ["A"]. Tostring ();
Application. Unlock ();
}

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 (20 minutes by default)
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. the user stops using the application.ProgramIt remains in the memory for a period of time, so this method is less efficient.
6. It is a good choice to save a small amount of data session objects.
7. sessions such as ASP and PHP depend on cookies. If the user does not accept cookies, the session cannot be used ;. net, as long as the web. if cookiesless is set to true in config, the session will be able to stand on its own.
8.Code:

// Storage Information
Session [ " Usernameid " ] = " 1000 " ;
// Read Information
String nameid = Session [ " Usernameid " ]. Tostring ();

Cookie 
1.Cookie is used to save the request information of the client browser requesting the Server Page.
2. Cookies store data in plain text on the client's computer. Therefore, it is best not to store sensitive unencrypted data.
3. You can set the retention period as needed,If no cookie expiration date is set, its lifecycle is saved until the browser is closed.
4.The expires attribute of the cookie object is set to minvalue, indicating that the object will never expire.
5. The volume of data stored in cookies is limited. Most browsers use 4 K. Therefore, datasets and other large amounts of data are not stored.
6. Not all browsers support cookies.
7.Code:

// Storage Information
Response. Cookie [ " Usernameid " ]. Value = " 1000 " ;
// Read Information
String nameid = Response. Cookie [ " Usernameid " ]. Value;

 

Httpcookie NC =   New Httpcookie ( " Newcookie " );
NC. Values [ " Name " ] =   " Aidd " ;
NC. Values [ " Age " ] =   " 22 " ;
NC. Values [ " DT " ] = Datatime. Now. tostring ();

Httpcookie getcook=Request. Cookies ["Newcookie"];
Response. Write (getcook. Values ["Age"]);

Viewstate
Session viewstate
Server resource occupation True False
Time out True False
Store any. Net Type True False (only strings, integers, booleans, arrays, arraylist, hashtable, custom typeconverters are supported)
Increase HTML load false true

The session is valid for the entire application, while the viewstate is equivalent to the session of a page.

1. It is often used to save the status information of a single user. The validity period is equal to the lifecycle of the page.
2. A large amount of data can be stored, but excessive use may affect the performance of applications.
3. All Web server controls use viewstat to save their status information during the page sending back.
4. Each control has its own viewstate. It is best to disable it when not in use to save resources.
5. You can disable the viewstate of the entire page by adding the "enableviewstate = false" attribute to the @ page command.
6. Code

// Save in viewstate
Viewstate [ " Sortorder " ] =   " Desc " ;
// Read from viewstate
String Sortorder = ( String ) Viewstate [ " Sortorder " ];

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 a user sends the same request, the server does not process it again, but directly returns the data saved in the cache to the user.
4.It can be seen that the time saved by the cache is the server processing time.
5.The cache instance is exclusive to each application, and its lifecycle = This application cycle; when the application restarts, its instance will be re-created.
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:

// Storage Information
Cache [ " Usernameid " ] = " 1000 " ;
// Storage Information
Cache. insert ( " Usernameid " , " 1000 " );
// Read Information
String usernameid = Cache [ " Usernameid " ]. Tostring ();

Hide domain
1. the hidden control is a server control of the HTML type (runat = server should be used), which can hide the domain function. It is no different from other controls but will not be displayed on the browser, always hidden.
2. Each time it is submitted, it will be submitted to the server together with other server controls.
3.Code:

// Storage Information
Hidden. Value = " 1000 " ;
// Read Information
String usernameid = Hidden. value;

Query string
Connect the passed value to the end of the URL and use the response. Redirect method to redirect the client.

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 " ];

 

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.