Common ASP. NET status management solutions include:
◆ View state
◆ Hide the Hidden Fields domain
◆ Cookies
◆ Query string
◆ Application state
◆ Session state
◆ Profile
ASP. NET Common state management, where View state, hidden fields, cookies, and query strings save values in different ways on the client. Application state, session state, and profile are saved on the server side in different forms.
View state)
View state automatically saves the values of each element on the web page, as long as the control's EnableViewState is true. viewState stores the value of an element in the form of a key-value pair. in asp.net, the value of view state is stored in the page by hashing in the form of hidden fields, which is similar:
- <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value=
"/wEPDwUJNzgzNDMwNTMzZGTN7+FHXsyXJ2Jnzu0UxkEXCEetrg==" />
If View state is used to store data, the data size cannot exceed the value defined by MaxPageStateFieldLength of page.
◆ Advantage: server-side resources are not required. Because view state is saved on the page, you do not need to borrow server-side resources to save the value. easy to use. You only need to save the value of an element in the form of a key-value pair. in terms of security, the value stored in view state is hashed and compressed, so it is safer to use than Hidden Fields.
◆ Disadvantage: performance problem. Because view state stores the value in Page, the value saved in view state is loaded every time the Page is loaded, resulting in performance loss.
Security issues. Although the value stored in view state is hashed and compressed, it still exists in the form of hidden fields on the page and is still easily intercepted and exploited by hackers.
Hidden Fields ):
This is an old way of saving element values in asp. the use of Hidden Fields depends on how the page submits data. the value of Hidden Fields can be obtained only after the Post method is submitted. If the Http Get method is used, the value of the Hidden element is invalid.
Another feature of Hidden Fields is that it saves element values in plain text in Html code. You can easily view the html source code of the page to obtain the value of the hidden fields element.
◆ Advantages: it is easy to use and widely used. Almost all browsers and client devices support hidden fields.
◆ Disadvantages: It is very insecure and can only store very simple data, such as string type and performance problems. den fields is used to load each page display, just like view state.
Cookies
Cookies are also a form of data storage on the client. Cookies can store data for a long time or temporarily, depending on the cookie expiration time settings. Cookies apply to the entire web site, rather than a page. They rely on browsers for management. If Cookies are disabled on the client browser, Cookies cannot be enabled to store data on the client. Cookies send data to the server along with page requests. The value of Cookies can be obtained through requests. Most browsers support Cookies to store 4 MB of data, and the browser also has limits on the number of Cookies that machines can hold. Generally, each site can generate up to 20 Cookies.
Read cookie value:
- If (Request. Cookies ["UserSettings"]! = Null)
- {
- String userSettings;
- If (Request. Cookies ["UserSettings"] ["Font"]! = Null)
- {UserSettings=Request. Cookies ["UserSettings"] ["Font"];}
- }
- Write value to Cookies:
- Response. Cookies ["UserSettings"] ["Font"] = "Arial ";
- Response. Cookies ["UserSettings"] ["Color"] = "Blue ";
- Response. Cookies ["UserSettings"]. Expires=DateTime. Now. AddDays (1d );
◆ Advantages: the expiration time can be configured, without occupying server resources, and is easy to use. It can also maintain persistent data.
◆ Disadvantage: size limit. Most browsers support 4 MB bytes. Restricted by user configuration. If the browser disables Cookies, this function cannot be used and may pose potential security risks. Cookies are stored in client machines as text files. Although the Cookies are hashed, they can still be obtained and used.
Query string
The query string is to add some parameters behind the URL of the page. In this form, the value between pages is passed. It provides a very convenient method for page value passing.
◆ Advantages:
Simple, convenient, and widely used
◆ Disadvantages: extremely insecure. It is necessary to use URLEncode and URLDecode to process strings to enhance security. The size is limited. Some browsers or client devices only support 2083 URL strings.
Application State
Asp.net allows you to store values in the form of Application state-an instance of the HttpApplicationState class. Application state provides a global storage method that can be obtained by every page of the web Application. The Application state is the same as the Session State. Values are stored as key-value pairs.
◆ Advantages: global scope
◆ Disadvantages: global scope, server resource occupation, and vulnerability. Because the Application State is stored in the memory, it will be lost when the Application is stopped or restarted. Poor scalability, cannot be shared among multiple servers or processors.
Session State
Session State is similar to Application state, but it acts on a browser Session. If different users use your application, different session sessions will be generated. Session state also stores data in the form of key/value pairs.
◆ Advantages: easy to use and data persistence. Because the Session provided by asp.net overcomes the dependency defect of the original asp process, the Session can be saved in the database, so the session data will never be lost. No cookie support is required. This is also a major improvement of asp.net. Session information of the client can be stored in Cookieless mode in ASP. NET.
◆ Disadvantage: The Session variable is stored on the server, so it occupies server resources.
Profile
Storing data in the form of Profile is a new feature provided by asp. net2.0. It stores information in the database, so it never loses information.
◆ Advantages: Data Persistence and scalability
◆ Disadvantages: Because the profile stores data in the database, it has the following features: manual data maintenance is required, and the performance is slightly poor.
The preceding section describes common ASP. NET status management.
- Analysis of Theme functions in ASP. NET development skills
- ASP. NET Dynamic Compilation
- Analysis on ASP. NET supported by Apache
- Introduction to ASP. NET Server standard controls
- Analysis on SQL Server Database Backup Recovery in ASP. NET