The following is a comparison of the objects that store various information in ASP. Understanding the principles of these objects is quite necessary for a well-developed program (pick to the Internet, not original--xukunping)
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 that each object applies to?
To get a clearer picture, we summarize the specific context in which each object is applied, as shown in the following table:
lifetime of a Web page
Method |
Information size |
save time |
Application scope |
save location |
Applicatio n |
any size |
lifetime of the entire application |
all users |
server-side |
Session |
Small amount, simple number |
User activity time + period of delay (general is 20 minutes) |
Single user |
server-side |
Cookie |
Small Volume, simple data |
can be set to |
single user |
client |
Viewstate |
Small, simple data |
Single user |
client |
Cache |
any size |
can be set as needed |
All users |
server-side |
hidden fields |
Small, simple data |
lifetime of a Web page |
Single user |
client |
query string |
small, simple data |
until next page jump request |
Single user |
client |
Web. config file |
unchanged or rarely changed small amount of data |
until configuration file is updated |
Single user |
server side |
1.Application objects
Application is used to hold public data information for all users, and if you use application objects, one of the issues to consider is that any write operations are in the Application_OnStart event (Global.asax) . Although the Application.Lock and Applicaiton.unlock methods are used to avoid synchronization of writes, they serialize requests for application objects, which can be severe when the site visits are large Bottlenecks. It is therefore best not to save large collections of data with this object.
2.Session objects
Session is used to save private information for each user. Her lifetime is the user's persistent request time plus a period of time (typically 20 minutes or so). S
The information in the ession is saved in the Web server content, and the amount of data saved can be large or small. The saved data information is automatically freed when the session expires or is closed. Because the user stops using the application, it remains in memory for a period of time, Therefore, using the session object makes it very inefficient to save user data. For small amounts of data, it is a good idea to use session objects to save. The code for saving information using the Session object is as follows:
Store information
session["username"]= "Zhouhuan";
Reading data
String username=session["UserName"]. ToString ();
3.Cookie objects
Cookies are used to store request information from a client's browser requesting a server page, and the programmer can use it to store non-sensitive user information, which can be set as needed. If the cookie expiration date is not set, They are only saved until the browser program is closed. If you set the Expires property of a cookie object to MinValue, the cookie will never expire. Cookies store very limited amounts of data, and most browsers support a maximum capacity of 4096, so do not use it to save datasets and other large amounts of data. Since not all browsers support cookies, and the data information is stored in clear text in the client computer, Therefore, it is best not to save sensitive, unencrypted data, otherwise it will affect the security of the site. The code saved with the cookie object is as follows:
Store information
response.cookies["UserID"]. Value= "0001";
Reading information
String userid=response.cookies["UserID"]. Value;
4.ViewState objects
ViewState is often used to hold status information for a single user, which is equal to the lifetime of the page. ViewState containers can maintain large amounts of data, but must be used with caution, because excessive use can affect the performance of the application. All Web server controls use ViewState to save their state information in a page postback tone. If a control does not need to save state information during a postback, it is a good idea to close the object's ViewState to avoid unnecessary waste of resources. You can suppress the ViewState of the entire page by adding the "Enableviewstate=false" property to the @page directive. The code for saving information using the ViewState object is as follows.
Store information
viewstate["NameID"]= "0001";
Reading information
String nameid=viewstate["NameID"]. ToString ();
5.Cache objects
The cache object is used to save pages or data between HTTP requests. The use of this object can greatly improve the efficiency of the entire application. It allows a large number of frequently accessed server resources to be stored in memory, and when the user makes the same request, the server does not process it again, but returns the information saved in the cache to the user, saving the server time to process the request. An instance of this object is dedicated to each application, and its lifetime depends on the lifetime of the application. When you restart the application, an instance of its cache object is recreated. The code for saving information using the cache object is as follows.
Store information
cache["NameID"]= "0001";
Store information
Cache.Insert ("NameID", "0001", 1);
Reading information
String nameid=cache["NameID"]. ToString ();
6. Hidden fields
The hidden control is a server control of the HTML type that can be used to implement the functionality of a hidden field. In fact, the use of this control and other server controls is not very different, except that it is not displayed in the client's browser and is always hidden. But each time the page commits, this control is committed to the server side with the other server controls, so you can use the Value property on the server side to get or save some data information. The code for saving information using the hidden control is as follows.
Store information
Hidden.value= "0001";
Get information
String Nameid=hidden.value;
7. Query string
The query string is done by connecting the value to be passed behind the URL, and then using the Response.Redirect method to implement client redirection. This approach allows information to be passed between two pages. Because the length of the URL is limited, it is not possible to pass too much information, and the addition of security is not very good.
The delivery information is as follows.
Response.Redirect ("list.aspx?nameid=0001&gradeid=002");
After executing the above statement, the code for the URL displayed in the IE Address bar follows.
http://localhost/List.aspx?nameID=0001&grade=002
When you jump to list.aspx, you can get the information you've passed through the following code.
String Nameid.gradeid;
nameid=request.params["NameID"];
gradeid=request.params["Gradeid"];
In addition to the several objects described above, you can also use the context object and the Web. config configuration file.
Asp. NET Save information Summary (application, Session, Cookie, viewstate, cache, etc.)