The HTTP protocol is a network protocol between requests and responses. It is different from the connection-based network protocol. For example, compared with the TCP protocol we are familiar with, the client and server do not have continuous connections, after each session, the connection is disconnected. In the next request, the client establishes a new connection with the server for the session. Then, how does the server track the client? How does the client maintain a session with the server while the client is disconnected?
To solve this problem, a large number of status management technologies are provided in website development. net, but also provides some dedicated technologies to make it easier for us to complete the State management service.
· First, we can considerHide domain. As the name suggests, hidden fields are input items that cannot be seen in the form. They must be used with the form. However, when a form is submitted, information in the hidden field is sent to the server along with the form. To hide a domain, use the following method:
<form method="post" action="hidden.aspx" id="form1">
...
<input type="hidden" id="HiddenField1" name="HiddenField1" value="DebugLZQ"/>
...
</form>
In ASP. NET, you can use the hiddenfield Server Control to generate the above HTML, so that we can obtain the hidden domain in the webpage on the server through the object-oriented method.
this.HiddenField1.value=this.TextBox1.Text;
By default, ASP. NET forms will be submitted to this page. Therefore, when submitting forms, information stored in the hidden domain of the client will be sent back to the server. Since the name of the gently parameter will match the ID of the hidden domain control in the page object on the server, in this way, in the next request, on the server, we can use the property of the hidden domain control to obtain the value stored in the hidden domain in the previous request.
this.TextBox1.Text=this.HiddenField1.value;
Problems: The Shadow domain exists in the Form in the webpage. When the page is closed, information in the hidden domain will be lost. Therefore, it is not suitable for long-term effective status management.
· To save the user session status information for a longer period of time,CookieIt was introduced into website development by Netscape.
Traditionally, websites are broadcast-based. That is to say, all users see the same content. If one-to-one services are provided to users, you must be able to access different users from user requests.Although this requirement can be partially fulfilled by hiding the domainHowever, when you close your browser, information stored in the hidden domain will be lost and cannot be saved for a long time. Cookie allows users to set their status informationStored in the client as a name-Value Pair. When a cookie is valid, it is automatically appended to the request every time the user sends a request to the server. It also allows you to set the time when the cookie is stored on the client, this allows you to save the status information after you close your browser.
In the HTTP protocol, you can add the set-Cookie response header to the server response to instruct the client to save the cookie.
The browser stores the cookie content in a special location, and the cookie content is not displayed on the page, which is suitable for storing the status information in a hidden manner.
When the browser saves a cookie, the browser will automatically append the information saved in the cookie in the form of a request header in future requests, it is sent to the server as a name-value pair.
In this way, the server can obtain the previously saved status information.
In ASP. NET, the server uses the httpcookie data type to process the problem of generating the set-Cookie header in the response and cookie information in the request.
public sealed class HttpCookie
With httpcookie, in httpresponse, the cookie set attribute is used to indicate the set-Cookie httpcookie set in the response. In this way, the code that instructs the client to generate a cookie on the server is as follows:
HttpCookie cookie=new HttpCookie("DebugLZQ");
cookie.Value=this.TextBox1.Text;
this.Reponse.Cookies.Add(cookie);
For the cookie information contained in the request, httprequest is represented as an object through its cookies set attribute. Because browser requests may contain multiple cookie parameters, you can obtain the corresponding cookie parameters by using the cookie name.
HttpCookie cookie=new HttpCookie("DebugLZQ");
if(cookie!=null)
{
this.Label1.Text=cookie.Value;
}
By default, the created cookie is saved in the cookie container of the browser. When the browser is closed, the cookie will be destroyed together. Expires attribute representation of cookieCookie expiration time. This is an absolute expiration time, expressed in Greenwich Mean Time Format. After this time, the cookie will become invalid. In order to use the information stored in the cookie for a long time, you can set the expires attribute of the cookie to a longer time in the future. The cookie will remain valid until this time. For example, you can use the following statement to save the cookie settings for 10 minutes.
cookie.Expires=DateTime.Now.AddMinutes(10.0);
NOTE: If no expiration time is set, the cookie will be saved until the browser is closed.
However, after the browser is closed, where will the cookie be saved? How can he still obtain the cookie value when he opens the browser again?
After setting the expiration time, the browser saves the cookie in a specific folder,Cookie file is a text file.. According to rfc2109, each client can store a maximum of 300 cookies. For each site, a maximum of 20 cookies can be stored. (In fact, most browsers have more than this cookie. For example, Firefox has 50 cookies ), each cookie can be 4 kb at most. Note that the 4 kb here may not be 4096 strict depending on different browsers.
· We can also useEmbed information in a URLTo achieve status management, because when sending a request to the server, the URL address will be sent as part of the request to the server, so that you can extract the saved information. When you need to save the information in the URL address, you can insert the information to the URL address in a special format.
· A large amount of data can be transmitted in a form using a hidden field. However, the information stored directly in the hidden field must be string-type data, which is determined by the character nature of the HTML page. The ASP. NET Server Processes data in the form of objects. If these objects can be saved for use in subsequent access, the server-side programming is much easier. In ASP. NETAdded state management mechanisms supported by Object serialization and deserialization Based on hidden DomainsBy serializing the serializable object on the server side as a string, and then saving it as a hidden domain in the also-sold form, it is generally sent back to the server in subsequent requests, finally, restore to the original object through the deserialization operation, in ASP. net is calledView status viewstate.
· Viewstate brings convenience to developers,However, the huge view status brings heavy network pressure during the round-trip transmission between the server and the browser.In some cases, we disable the view status that is also sold out to improve access efficiency.
Some information is very important to deal with controls, so,DisableViewstateSome controls will be permanently invalid, such as the gridview.Starting from ASP. NET 2 0, the control state is used to provide a more secure way than the view State.Control status controlStateManagement mechanism. The usage of the control status is similar to the view status, but it will still be valid even after the user closes the view status.
Similar to the loadviewstate and saveviewstate methods of the view state, the control state also provides a pair of such methods. The methods are named loadcontrolstate and savecontrolstate respectively, and the weapon is also provided in the control base class.
To use the control status, you must first register the status of the control you want to use on the display. This work is generally carried out in the control's init. The method parameter is the control object to be registered.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
page.RegisterRequiresControlState(this);
}
Then, use loadviewstate and saveviewstate in the control to load and save the control state.
· Application StatusHttpapplicationstateThe Mechanism is relatively simple. This is the global state management of a server. Because it is the global state management of the server, it does not involve the process of both selling and request processing, nor need to distinguish different sessions or users. To facilitate saving multiple different states, the application state application is derived from nameobjectcollectionbase, allowing you to save or extract the corresponding objects through a string name.
The application state management object can be obtained through the context object. Its definition is as follows:
public HttpApplicationState Application{get;}
The definition of httpapplicationstate is as follows:
public sealed class HttpApplicationState:NameObjectCollectionBase
For reference program status objects, because all requests may need to access this object, in order to solve the problem of concurrent access, it also provides a thread synchronization mechanism throughLockTo synchronize access to this set.
public void Lock();
public void UnLock();
The httpapplicationstate class uses the allkeys and count attributes and the ADD, clear, get, getkey, remove, removeat, and set methods to automatically lock and unlock. However, if you need to perform multiple consecutive accesses to the data stored in the global application state, locking and unlock through explicit locks can help improve access efficiency.
For a website, there is an httpapplicationstate object management state. This object is worn as the website starts and will be released only when the website is closed. Therefore, the status information saved through the application is accompanied by the lifecycle of the entire website. When it is not applicable, you must release the space in time.
·SessionIt has a long history of state management technology. Since the ASP era, it has become an indispensable basic State Management Technology for web development.
For stateless HTTP, session is a server-side state management solution, which features that each client can save the actual data on the server. For client data, A unique key is generated. After the server saves the data, the key used to obtain the stored data is sent to the client. In subsequent requests, the client sends the key stored on the client back to the server and obtains the corresponding data through the key on the server. Generally, we call this key sessionid. The session works as follows:
Generally, this sessionid is saved in the browser as a cookie. If the cookie is not applicable, you can also embed this sessionid into the URL address of the website you want to access (as mentioned earlier ).
In the summary of page objects or httpcontext objects, there is a property named session. In a session, they reference the same object and are defined as follows:
public HttpSessionState Session{get;}
In sesion, we usually use the following method for State management:
public void Add(string name,Object value)
public void Remove(string name);
public void Clear();
public void Abandon();
The httpsessionstate is from sessionstatemodule. During each request processing, the request processing pipeline of httpapplication checks whether the current request processing program implements the irequiressessionstate interface. If so, the sessionstatemodule assigns an httpsessionstate to the request.
It should be noted that the session starts only after executing the following code snippet.
Session[“DebugLZQ”]=”some data”;
The session dictionary usually contains the object type. to read data backward, You need to convert the returned value to a more specific type.
string data=Session[“DebugLZQ”] as string;
The session_onend event indicates the end of the session and is used to execute all the cleanup Code required to abort the session.
If a session times out or is abandoned, its session ID will not change the next time you access a stateless application. After the design, even if the session status expires, the session ID can continue until the browser session ends. That is to say,The same session is always used as long as the browser instance is the sameIDIndicates multiple sessions.
For sessions, the key issue lies inSession expiredIt refers to the expiration of a session's entire session and does not expire for a specific data. Either the entire session is discarded after expiration, or all data is retained, each request is loaded into the memory, which may cause a waste of memory. Therefore, when using session, we must carefully select the data to be saved.
·Httpcontext statusIt may be the simplest and most effective State management method in all State management. httpcontext is based on the processing pipeline of httpapplication. Because the httpcontext object runs through the entire processing process, status data can be transferred from the front end of the httpapplication MPs queue to the backend of the MPs queue to complete the status transfer task.
In httpcontext, the items attribute allows us to access the data to be transmitted. The attribute is defined as follows:
public IDictionary items{get;}
We can see that this is a dictionary. We can use it in the form of a key-value pair.
·Cache Management CacheIs a more powerful state management in ASP. NET. This is the only way in ASP. NET to dynamically manage the status of memory usage based on server usage. We can set effective conditions for each cached data to maximize the memory usage efficiency. Cache is a public State management. We use the key-value strings of each cached data to differentiate the cached data (not detailed ~).
·MemcachedIt is a distributed memory object cache system developed by dangga.com. Memcached is distributed. It is an application-independent program or daemon based on network connections (localhost can also be used.
Memcached is often used as the database front-end cache. Because he has much less overhead than the database, such as SQL parsing and disk operations, and uses memory to manage data, he can provide better performance than reading the database directly. In large systems, the same data is frequently accessed. memcached can greatly reduce the database pressure and improve the system execution efficiency.
[Click "Green Channel"-"belowFollow debuglzq", Learning and improving together with debuglzq ~]