Status management in ASP.NET

xiaoxiao2021-03-06  64

We can easily solve these problems through cookies, query strings, applications, dialogue, etc. in ASP. Now in the ASP.NET environment, we can still use these features, but they are more types, and the functions are more powerful.

There are two different ways to manage Internet webpages: clients and server.

1, the state management of the client:

In the client, multiple requests between the servers - the response period, the information is not saved on the server, and the information will be stored on a web page or user's computer.

A, cookie

Cookies are a small amount of data stored in the text file of the client file system or the memory of the client browser conversation, which is mainly used to track data settings. Below us, however, suppose we want to customize a welcome Internet page, when the user requests the default Internet page, the application first checks the user before or not, we can get the user from cookies:

[C #] IF (Request.Cookies ["UserName"]! = NULL) LBMessage.Text = "Dear" Request.Cookies ["UserName"]. Value ", Welcome Shopping Here!"; elselbMessage.Text = "Welcome Shopping Here! ";

If you want to store the user's information, we can use the following code:

[c #] response.cookies ["username ']. value = usrname;

This way, when the user requests the web page, we can easily identify the user.

B, hidden domain

The hidden domain does not appear in the user's browser, but we can set its properties like setting the standard control. When a web page is submitted to the server, the contents of the hidden domain and other controls are sent to the HTTP Form collection. The hidden domain can be any repository that stores information related to the web page in the web page, hiding the domain stores a variable in its value attribute, and must be explicitly added on the web page.

The HTMLINPUTHIDEN control in ASP.NET provides the function of hiding the domain.

[C #] protected system.Web.ui.htmlcontrols.htmlinputhidden hidden1; file: // Assign hidden1.value = "this is a test"; file: // Get a hidden domain String str = Hidden1.Value ;

It should be noted that the use of hidden domains must be submitted to the Internet page using the http-post method. Although its name is a hidden domain, its value is not hidden, we can find its value through the "View Source" function. C, status view

Each control includes a web forms page, a name for ViewState, which is an internal structure of the automatic web page and control status, which means that we don't have to take any time after submitting a web page to the server. Measures to restore the control data. Here, for the viewState property of us, we can use it to save multiple requests between multiple times between the servers.

[C #] file: // Save Information ViewState.Add ("Shape", "Circle"); file: // Get Information String Shapes = ViewState ["Shape"];

Note: Different from the hidden domain, when you use the View source code function, the value of the ViewState property is invisible, which is compressed and encrypted. D, query string

The query string provides a simple and restricted maintenance status information. We can easily pass the information from a web page to another, but most browsers and client devices limit the length of the URL at 255. The character is long. In addition, the query value is passed to the Internet via the URL, so in some cases, security has become a big problem. The URL with the query string is as follows:

http://www.examples.com/list.aspx?categoryid=1&productId=101

When there is a client request List.aspx, you can get directory and product information by the following code:

[c #] string categoryid, productId; categoryid = request.params ["categoryID"]; productId = request.params ["ProductID"];

Note that we can only submit the Internet page with http-get, otherwise you cannot get the required values ​​from the query string. 2, server-side state management

Information is stored on the server, although its security is high, it will occupy more web server resources.

A, APLICATION object

The APLICATION object provides a mechanism that allows all code access to code accessed in a web application server, and inserts data inserted into the application object status variable should be shared by multiple conversations, and will not change frequently. It is precisely because it can be accessed by all applications, so we need to use Lock and Unlock to avoid conflicts.

[c #] Application.lock (); Application ["mydata"] = "mydata"; application.unlock ();

B, session object

The session object can be used to store information that requires a specified conversation that needs to be maintained during the multiple request-response period and the request for the web page. The session object is the basis of the existence of each conversation, that is, different clients generate different session objects. The period of data stored in the conversation status variable is shorter.

The ASP.NET conversation of each activity is uniquely determined and tracked by a SessionID string containing legitimate URL ASCII characters with a length of 120. The value of SessionID is generated by an algorithm that guarantees uniqueness so that the conversation will not conflict, and the parasis of sessionid makes it difficult to guess the ID of the existing dialogue.

Depending on the configuration settings of the application, the sessionID is transmitted between the client-server request via HTTP cookie or the modified URL. So how do you set the dialogue equipment method for the application configuration.

Each web application must have a named web.config configuration file, which is based on an XML file. Below is a dialogue named sessionState:

The value of the cookieles option 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 randomly generate a unique number and put it in requested. In front of the file, this number is used to identify users, we can see it in the address bar of IE:

http: // localhost / management / (2yzakzez3eqxut45ukyzq3qp) /Default.aspx OK, let's go back to the session object.

[c #] file: // Storage information session ["myname"] = "mike"; file: // get information myname = session ["myname"];

C, database

The database will allow us to store information related to the status in the web application, sometimes, users will frequently access the database frequently, we can store it in the database, multiple requests to the web page Used. Summarizing the features and tools in ASP.NET more than the ASP, allowing us to manage the status of web pages more efficiently and efficiently. Specifically, which method is related to your application, you can consider the following questions when you choose:

· How much information is required? · The client accepts a lasting or cookie in memory? · I hope to save the letter on the server or on the server? · Is the information to be stored confidential? · What is the performance of your web page?

转载请注明原文地址:https://www.9cbs.com/read-113071.html

New Post(0)