HTTP is a stateless protocol. Each request is independent, and its execution and results are not directly related to the previous request and the subsequent request, and it will not be directly affected by the response to the previous request, nor will it directly affect the response to the subsequent request.
In fact, our system often supports users to share the same data (state) between the client browser and the server multiple requests, such as the user's login account information. Therefore, ASP.NET provides many variables to manage the state: application state, session state, view state, etc.
The HttpContext object is only for a single http request.The properties of this class also include Request objects, Response objects, Session objects, etc. This is the Items collection of the HttpContext class, which contains a hash table object in the form of key-value.
First, let's look at the purpose of HttpContext.Current.Items, which only works on a single user request (HttpContext.Current.Items valid for a single HTTPRequest). When this request is completed, the item collection will be lost when the server information is sent back to the browser. The Session object is for the user's session, that is, it acts on multiple user requests, and the information is lost after the session expires.
Since HttpContext.Current.Items has such a short lifecycle, under what circumstances can it be used? It is noted here that HttpContext.Current.Items can be used when sharing data between HttpModule and HTTPHandler, because every user request goes through the HTTP runtime pipeline HttpModule, HTTPHandler. When you implement the IHttpMoudle method to pass information to the user request via HttpMoudle. You can use HttpContext.Current.Items to transfer data in different request pages and different HttpModules, but once the request ends and the data is posted, the data in this collection will be lost by itself. As shown in the following figure:
In addition, when the server page jumps (Server.Execute/Server.Transfer), we can use HttpContext.Current.Items to pass data between the two forms.
Obviously, if you change Server.Transfer to Response.Redirect, you will not be able to get the data in HttpContext.Current.Items in the new page because it is a different Http request. System.NullReferenceException: The object reference is not set to the object's instance.
|