Asp. The state management in net

Source: Internet
Author: User
Tags configuration settings file system http cookie unique id
asp.net we can easily solve these problems in ASP with cookies, query strings, applications, dialogs, and so on. Now in the asp.net environment, we can still use these features, but they are more diverse and more powerful.
There are two different ways to manage Internet Web pages: client and server side.

1, the client's state management:

During multiple requests-replies between the client and the server, the information is not saved on the server, and the information is stored on the Web page or on the user's computer.

A, cookies

A cookie is a small amount of data stored in the client file system's text file or in the memory of the client browser dialog, which is used primarily to track data settings. Here are some examples: Suppose we want to customize a welcome Internet page, when the user requests the default Internet page, the application will first check whether the user has been registered before, we can get the user information from the cookie:

[C #]
if (request.cookies["username"]!=null)
lbmessage.text= "Dear" +request.cookies["username"]. Value+ ", Welcome shopping here!";
Else
lbmessage.text= "Welcome shopping here!";

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

[C #]
response.cookies["Username '". Value=username;

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

B, hidden fields

Hidden fields do not appear in the user's browser, but we can set their properties as if they were standard-controlled. When a Web page is submitted to the server, the contents of the hidden field and other controlled values are sent to the HTTP form collection. A hidden field can be any repository of page-related information stored in a Web page, where a hidden field stores a variable in its Value property and must be explicitly added to the Web page.

Asp. NET provides the function of hidden domain in HtmlInputHidden control.

[C #]
protected System.Web.UI.HtmlControls.HtmlInputHidden Hidden1;
file://to a hidden field
Hidden1.value= "This is a test";
file://gets the value of a hidden field
String Str=hidden1.value;

It is important to note that to use a hidden field, you must submit an Internet page using the Http-post method. Although its name is a hidden field, but its value is not hidden, we can find its value through the "View Source code" feature.
C, Status View

Each control on the Web Forms page, including the Web page itself, has an attribute named ViewState, which is a built-in structure that automatically maintains the page and control state, which means that after submitting a page to the server, we do not need to take any action to recover the controlled data.
Here, what's useful for us is the ViewState attribute, which we can use to hold the information from multiple requests to the server during the reply.

[C #]
file://Save Information
Viewstate.add ("Shape", "circle");
file://Get information
String shapes=viewstate["shape"];

Note: Unlike hidden fields, the values of the ViewState property are not visible when you use the view source functionality, and they are compressed and encrypted.
D, query string

The query string provides a simple and restricted way to maintain state information, and we can easily pass information from one Web page to another, but most browsers and client devices limit the length of the URL to 255 characters long. In addition, the query value is passed through the URL to the Internet, so in some cases, security becomes a big problem.

The URL with the query string looks like this:

Http://www.examples.com/list.aspx?categoryid=1&productid=101

When a client requests list.aspx, you can obtain the catalog and product information by using the following code:

[C #]
String CategoryID, ProductID;
Categoryid=request.params["CategoryID"];
Productid=request.params["ProductID"];

Note that we can only use Http-get to submit the Internet page, otherwise we cannot get the desired value from the query string.
2, server-side state management

Information is stored on the server and, despite its high security, consumes more Web server resources.

A, Aplication objects

The Aplication object provides a mechanism for storing data that is accessed by code that runs on the Web application server, and data inserted into the Application object state variable should be shared by multiple dialogs and will not change frequently. Precisely because it is accessible to all applications, we need to use lock and unlock to avoid conflicting values.


[C #]
Application.Lock ();
application["MyData"]= "MyData";
Application.UnLock ();

B, Session Object

The session object can be used to store information about a specified conversation that needs to be maintained during a multiple request-reply of the server and during a request to a Web page. The session object is the basis for the existence of each dialog, which means that different clients generate different sessions objects. The data stored in the dialog state variable has a shorter period of time.

Each active asp.net dialog is uniquely identified and tracked by a SessionID string that contains a valid URL ASCII character, a length of 120 bits. The SessionID value is generated by an algorithm that guarantees uniqueness, so that there is no conflict between dialogs, and the randomness of SessionID makes it difficult to guess the ID of an existing conversation.

Depending on the configuration settings of the application, SessionID is transmitted between the client-server requests via an HTTP cookie or a modified URL. So, how to set the dialog device 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 dialogue named sessionstate:


The value of the cookieless option is true or false. When the value is False (the default), the ASP. NET uses an HTTP cookie to identify the user, and when the value is true, the ASP. NET randomly generates a unique number and puts it in front of the requested file, which is used to identify the user, and we can see it in the IE's Address bar:

http://localhost/Management/(2YZAKZEZ3EQXUT45UKYZQ3QP)/default.aspx
OK, let's go back to the session object below.

[C #]
file://Storage Information
session["MyName"]= "Mike";
file://access to Information
myname=session["MyName"];

C, Database

The database will allow us to store a large amount of information related to state in the Web application, and sometimes users will access the database with a unique ID, which we can store in the database and use in multiple requests to the pages in the Web site.
Summarize

Asp. NET has more functions and tools than ASP, enabling us to manage the state of Web pages more efficiently and efficiently. Choose which method is relevant to your application and consider the following questions when choosing:

? How much information do you need to store?
? Does the client accept persistent or in-memory cookies?
? Do you want to store the letter on the client or on the server side?
? Does the information you want to store need to be kept secret?
? How do you want your Web page to function?



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.