These objects are used to hold information, including user information, passing value information, global information, and so on. The difference between them:
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) is completed. Although the Application.Lock and Applicaiton.unlock methods are used to avoid synchronization of the write operation, it has serialized the request for the Application object. Serious performance bottlenecks occur when the site is heavily visited. It is therefore best not to save large data collections with this object. Let's do an online user statistics example to illustrate the problem:
(Total number of site visits as a file)
Global.asax class
Code
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Web;
Using System.Web.SessionState;
Using System.IO;
Summary description of Global.
public class Global:System.Web.HttpApplication
{
The required designer variables.
Private System.ComponentModel.IContainer components = null;
Private FileStream FileStream;
Private StreamReader reader;//Read character stream
Private StreamWriter writer;//Writing stream
Public Global ()
{
InitializeComponent ();
}
protected void Application_Start (Object sender, EventArgs e)
{
application["currentguests"]=0;//initial flower is 0;
FileStream = File.Open (Server.MapPath ("Counts.text"), filemode.openorcreate);//file does not exist, create file
reader = new StreamReader (fileStream);//The full path to read
application["allguests"] = Convert.ToInt32 (reader. ReadLine ()); Reads a line of characters from the current stream and returns the data as a string
Reader. Close ();//Closed stream
}
protected void Session_Start (Object sender, EventArgs e)//When users visit the site, online users +1, total visits +1
{
Application.Lock ();//Synchronize to avoid simultaneous writing
application["currentguests"] = (int) application["currentguests"]+ 1;//total number of online users
application["allguests"] = (int) application["allguests"]+ 1;//total number of users visiting the site
FileStream = new FileStream (Server.MapPath ("Counts.text"), filemode.openorcreate,fileaccess.readwrite);//
writer = new StreamWriter (fileStream);//implement a write stream so that it writes characters to the stream in a specific encoding
Writer. WriteLine (application["allguests"). ToString ());//write the total number of users who visit the site again to the file
Writer. Close ();//Shutdown Write stream
Application.UnLock ();//sync End
}
protected void Session_End (Object sender, EventArgs e)//When the current user exits the site, the number of online users-1,
{
Application.Lock ();
application["currentguests"] = (int) application["currentguests"]-1;//total number of online users-1
Application.UnLock ();
}
(2) WebForm1.aspx
private void Page_Load (object sender, System.EventArgs e)
{
This. Label1.Text = "Number of users who are accessing the site:" + application["currentguests"]. ToString ();
This. Label2.Text = "Total number of users who have visited the site:" + application["allguests"]. ToString ();
}
2.Session objects
Session is used to save private information for each user. When each client user accesses, the server assigns a unique session ID to each user (the session ID). Her lifetime is the user's persistent request time plus a period of time (typically 20 minutes or so). The information in the session 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["Key"]= "value"
Reading data
String username=session["key"]. 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 are limited in the amount of data stored, and most browsers support a maximum capacity of 4K. So don't use it to save datasets and lots of other data. Because not all browsers support cookies, and the data information is stored in clear text in the client computer, it is best not to save sensitive, unencrypted data, Doing so will affect the security of your site. The code saved with the cookie object is as follows:
Store information
response.cookies["Key"]. Value= "Value";
Reading information
String userid=response.cookies["key"]. 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. Similar to hidden controls. ViewState is the value of each function in this page, as to why to use this method is because after an event occurs, the page may be refreshed, if the definition of global variables will be zeroed, so use viewstate. 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["Key"]= "value";
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. Often used to store a large number of frequently accessed server resources in memory, 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. 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. Question mark after format key1=value1&key2=value2
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"];
The difference between ASP. Application, Session, Cookie, ViewState, Cache, Hidden