In ASP, we can use cookies, query strings, and applications. Program And dialog to easily solve these problems. Now in the ASP. NET environment, we can still use these features, but they are more diverse and more powerful.
There are two main ways to manage Internet web pages: client and server.
1. Client status management:
During multiple request-response periods between the client and server, the server does not save the information, and the information will be stored on the webpage or user's computer.
A. Cookie
Cookie is a small amount of data stored in the text file of the client file system or in the memory of the client browser conversation. It is mainly used to track data settings. The following is an example: Suppose we want to customize a welcome Internet webpage. When a user requests a default Internet webpage, the application will first check whether the user has registered before, we can obtain user information from cookies:
[C #]
If (request. Cookies ["username"]! = NULL)
Lbmessage. Text = "dear" + request. Cookies ["username"]. Value + ", welcome shopping here !";
Else
Lbmessage. Text = "welcome shopping here !";
To store user data, we can use the followingCode:
[C #]
Response. Cookies ["username"]. value = username;
In this way, when the user requests the webpage, we can easily identify the user.
B. Hide the domain
The hidden domain is not displayed in the user's browser, but we can set its attributes like setting the property controlled by the standard. When a webpage is submitted to the server, the hidden domain content and other controlled values are sent to the HTTP form set together. A hidden domain can be a repository of Webpage-related information stored on a webpage. A hidden domain stores a variable in its value attribute and must be explicitly added to the webpage.
The htmlinputhidden control in ASP. NET provides the function of hiding a domain.
[C #]
Protected system. Web. UI. htmlcontrols. htmlinputhidden hidden1;
File: // assign values to hidden fields
Hidden1.value = "this is a test ";
File: // obtain the value of a hidden field
String STR = hidden1.value;
Note that to use a hidden domain, you must use the http-POST method to submit an Internet webpage. Although its name is a hidden field, its value is not hidden.Source code"Function to find its value.
C. view the status
Each control on a web forms page, including the webpage itself, has a property named viewstate. It is a built-in structure that automatically maintains the webpage and control status, this means that after submitting a webpage to the server, we do not need to take any measures to restore the controlled data.
Here, the viewstate attribute is useful for us. We can use it to store information during multiple request-response periods with the server.
[C #]
File: // save information
Viewstate. Add ("shape", "circle ");
File: // get information
String shapes = viewstate ["shape"];
Note: Unlike hidden fields, viewstate attributes are invisible and compressed and encrypted when you use the view source code function.
D. query strings
The query string provides a simple and restricted maintenance state information method. We can easily transfer information from one web page to another, however, most browsers and client devices limit the URL length to 255 characters. In addition, the query value is transmitted to the Internet through a URL. Therefore, in some cases, security becomes a major problem.
The URL with the query string is as follows:
Http://www.examples.com/list.aspx? Categoryid = 1 & productid = 101
After a client requests list. aspx, you can use the following code to obtain the Directory and product information:
[C #]
String categoryid, productid;
Categoryid = request. Params ["categoryid"];
Productid = request. Params ["productid"];
Note: You can only use HTTP-get to submit the Internet webpage. Otherwise, you cannot obtain the required value from the query string.
2. Server Status Management
Information is stored on the server. Despite its high security, it occupies a large amount of Web Server resources.
A. aplication object
The aplication object provides a mechanism for all code running on the Web application server to access the stored data. data inserted into the state variables of the Application object should be shared by multiple dialogs, and will not change frequently. Because it can be accessed by all applications, we need to use the lock and unlock pairs to avoid conflicting values.
[C #]
Application. Lock ();
Application ["mydata"] = "mydata ";
Application. Unlock ();
B. Session Object
The Session object can be used to store the information of a specified dialog that needs to be maintained during multiple server request-response and webpage requests. The Session object is the basis for the existence of each conversation, that is, different clients generate different session objects. The data stored in the dialog state variable has a short period of time.
The ASP. Net dialog of each activity is uniquely identified and tracked by a sessionid string containing valid url ascii characters and 120 characters. The sessionid value is unique byAlgorithmGenerated so that there is no conflict between conversations. the randomness of sessionid makes it difficult for us to guess the ID of an existing conversation.
According to the application configuration, sessionid is transmitted between client-server requests through HTTP cookies or the modified URL. Then, how to set the dialog equipment Method for application configuration.
Each web application must have a configuration file named Web. config, which is based on an XML file. The following is a dialog named sessionstate:
The value of cookieless is true or false. When its value is false (default), Asp. net will use HTTP cookie to identify users; when its value is true, Asp. net will generate a unique number randomly and put it in front of the requested file. This number is used to identify the user and we can see it in the address bar of IE:
Http: // localhost/management/(2yzakzez3eqxut45ukyzq3qp)/default. aspx
OK. Next we will return to the session object.
[C #]
File: // storage Information
Session ["myname"] = "Mike ";
File: // obtain information
Myname = session ["myname"];
C. Database
The database enables us to store a large amount of State-related information in Web applications. Sometimes, users frequently access the database using a unique ID, and we can store it in the database, used in multiple requests to webpages on the website.