In ASP. NET, there are many types of objects that store information. For example, 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 |
amount of information |
Save time |
Application Scope |
Save location |
| application |
any size |
lifetime of the Program |
All Users |
server |
| session |
small and simple data |
user activity time + Delay Time (generally 20 minutes) |
Single User |
server |
| cookie |
small and simple data |
set as needed |
Single User |
client |
| viewstate |
small and simple data |
life cycle of a Web page |
Single User |
client |
| cache |
any size |
set as needed |
All Users |
server |
| hide a domain |
small and simple data |
life cycle of a Web page |
Single User |
client |
| query string |
small and simple data |
wait until the next page Jump request |
Single User |
client |
| Web. config file |
a small amount of data that remains unchanged or rarely changes |
until the configuration file is updated |
Single User |
server |
Application
1. application is used to save information shared by all users;
2. In the ASP era, if the data to be stored does not or rarely change during the lifetime of the application, 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 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, resulting in severe performance bottlenecks;
4. Do not use application to save large data volumes;
5,Code:
- Application ["Userid"] ="Test";// Write
- StringUsername = application ["Userid"]. Tostring ();// Read
Session
1. Session is used to save the proprietary information of each user;
2. The session lifetime is the duration of the user's sustained request plus a period of time (generally about 20 minutes );
3. session information is stored in the memory of the Web server, and the stored data volume can be large or small;
4. Data is automatically released when the session times out or is disabled;
5. This method is inefficient because it remains in the memory for a period of time after the user stops using the application;
6. Code:
- Session ["Userid"] ="Test";// Write
- StringUsername = session ["Userid"]. Tostring ();// Read
Cookie
1. Cookies are used to save the request information sent from the client browser to the server page;
2. We can store non-sensitive user information and set the storage time as needed;
3. If no cookie expiration date is set, its lifecycle will be saved until the browser is closed;
4. Setting the expires attribute of the cookie object to minvalue indicates that the object will never expire;
5. The volume of data stored in cookies is limited. Most browsers use 4 K, so do not store enlarged data;
6. Because not all browsers support cookies, data will be stored on the client in plain text;
7. Code:
- Resopnse. Cookies ["Userid"] ="Test";// Write
- StringUsername = resopnse. Cookies ["Userid"]. Tostring ();// Read
Viewstate
1. viewstate is used to save the user's status information. The validity period is equal to the lifecycle of the page;
2. You can store a large amount of data but use it with caution because it will affect program performance;
3. All Web server controls use viewstate to save the status during page PostBack;
4. Disable enableviewstate = false in @ page if not required;
5. Code:
- Viewstate ["ID"] ="Test";// Write
- StringId = viewstate ["ID"]. Tostring ();// Read
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 storing frequently accessed server resources in the memory. When a user sends the same request, the server directly returns the data stored in the cache instead of processing it again;
4. the cache saves time (server processing time );
5. the cache instance is exclusive to each application. its lifecycle = This application cycle. When the application restarts, it 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:
- Cache ["ID"] ="Test"; Or cache. insert ("ID","Test");// Write
- StringId = cache ["ID"]. Tostring ();// Read
Hidden
1. The hidden control is an HTML-type server control and is always hidden;
2. Each time it is submitted, it will be submitted to the server together with other server controls;
3. Code:
- Hidden. value ="King";// Write
- StringId = hidden. value; Use runat = Server// Read
Query string
1. The string query method is to connect the value to be passed to the URL;
2. It is generally used to transmit information between pages;
3. Because the URL length is limited, too much information cannot be transmitted;
4. Poor security.
5. Code:
- Response. Redirect ("List. aspx? Id = 123 & name = ABC");// Value transfer page
- StringName = request. querystring ["Name"];StringId = request. querystring ["ID"];// Value page
Application
1.ApplicationUsed to save information shared by all users
2.InASPIn the ageApplicationIs the ideal choice. HoweverAsp.netIn the development environment, we put similar configuration data inWeb. config.
3.If you want to useApplicationNote that all write operations must be performed inApplication_onstartCompleted in the event (Global. asax).Application. Lock ()Conflicts are avoided, but it serializesApplicationWill produce serious performance bottlenecks.
4.Do not useApplicationStore large data volumes
5.Code:Application ["userid"] = "test ";
String username = application ["userid"]. tostring ();
Session
1.SessionUsed to save the private information of each user
2.SessionThe lifetime is the user's continuous request time plus a period of time (generally20About minutes)
3.SessionThe information is stored inWebIn the server memory, the stored data volume can be large or small
4.SessionData will be automatically released when the timeout or shutdown times out.
5.This method is inefficient because it remains in memory for a period of time after the user stops using the application.
6.Code:Session ["userid"] = "test ";
String username = session ["userid"]. tostring ();
Cookie
1.CookieUsed to save the request information of the client browser request Server Page
2.We can store non-sensitive user information and set the storage time as needed.
3.If noCookieExpiration date, whose lifecycle ends when the browser is closed
4.CookieObjectExpiresSet propertyMinvalueIndicates that it never expires.
5.CookieThe amount of data stored is limited, and most browsers4 KTherefore, do not store magnified data.
6.Not all browsers supportCookie, Data will be stored in plain text on the client
7.Code:Resopnse. Cookies ["userid"] = "test ";
String username = resopnse. Cookies ["userid"]. tostring ();
Viewstate
1.ViewstateUsed to save the user's status information. The validity period is equal to the lifecycle of the page.
2.Large amounts of data can be stored, but use it with caution because it will affect program performance.
3.AllWebServer controls are all usedViewstateOn the pagePostBackSave status
4.Disable if no@ PageSet inEnableviewstate = false
5.Code:Viewstate ['"Id"] = "yiner ";
String id = viewstate ["ID"]. tostring ();
Cache
1.CacheUsed inHTTPSave page or data during request
2.CacheCan greatly improve the efficiency of the entire application.
3.It allows you to store frequently accessed server resources in the memory. When the user sends the same request
The server does not processCacheThe stored data is directly returned to the user.
4.We can see thatCacheSave time-Server Processing Time
5.CacheAn instance is proprietary to every application and its lifecycle=Application cycle
Restart the application and re-create its instance
6.Note: To use functions such as cache cleanup, expiration management, and dependency items, you must useInsertOrAddMethod to add information
7.Code:Cache ['"Id"] = "yiner ";OrCache. insert ("ID", "test ");
String id = cache ["ID"]. tostring ();
Hidden
1.HiddenControl belongsHtmlType of server controls, always hidden
2.Each time it is submitted, it will be submitted to the server together with other server controls.
3.The Code is as follows:Hidden. value = "King ";
String id = hidden. value; Use runat = Server
Appendix 1:
Application_start: This method is triggered when the first user accesses the website. this method usually defines some system variables, such as the total number of online users in the chat room, and the initialization of historical Visitor statistics.
Application_end: This method is triggered when the website is closed or restarted. Too many tests are not performed.
Session_start: triggered when each user accesses the first page of the website;
Session_end: the session is used. abandon (), or Session Timeout can be triggered after the user exits. note that session objects, server objects, and servers cannot be used in session_end. mappath (). so you can only use session. CONTENTS ("username") replaces SESSION ("username "). or use the application object or method;
For example, you want to use server in session_end. mappath ("users. application ("xmlpath") = server. mappath ("users. XML "), and then call application (" xmlpath ") in session_end, instead of directly using server in session_end. mappath ("users. XML "),
Their execution sequence is: 1: application_start 2: session_start; 3: session_end: 4 application_end