Use of HttpContext.Current.Items

Source: Internet
Author: User

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 affected directly by the previous request response, nor will it directly affect the subsequent request response situation.
In fact, our system often supports users to share the same data (state) in multiple requests between the client browser and the server, such as the user's login account information. Thus, ASP. NET provides a number of variables to manage the state: Application State,session State,view, for example.
The HttpContext object is only for a single HTTP request. The properties of this class are the request object, the response object, the session object, and so on. Here is the HttpContext class's Items (properties) collection, which contains the hash table object in the form of Key-value.

First, we look at the use of HttpContext.Current.Items, which only makes a single user request (HttpContext.Current.Items valid for a). When this request is completed, the server information is passed back to the browser and the item collection is lost. And the session object is for the user of this conversation, that is, acting on a number of user requests, after the session failed to lose the information.


Since the life cycle of HttpContext.Current.Items is so short, under what circumstances can it be exploited?
It is noted here that HttpContext.Current.Items can be used when sharing data between HttpModule and HttpHandler because each user request is HttpModule, HttpHandler through the HTTP runtime pipeline. When you implement the Ihttpmoudle method to request a message through Httpmoudle to the user. You can use HttpContext.Current.Items to transfer data on different request pages, in different HttpModule, but once the request is over and the data is sent back, the data in this collection will be lost. As shown in the following:


In addition, when a service-side page jumps (server.execute/server.transfer), we can use HttpContext.Current.Items to pass data between two forms.

/** * WebForm1: * **/Private voidPage_Load (Objectsender, System.EventArgs e) {ArrayList list=NewArrayList (4); List. ADD ("This list"); List. ADD (" is for"); List. ADD ("WebForm2"); List. ADD ("To see ."); context.items["webform1list"] =list; Server.Transfer ("webform2.aspx");}/** * for WEBFORM2: * **/Private voidPage_Load (Objectsender, System.EventArgs e) {ArrayList list= context.items["webform1list"] asArrayList; foreach(stringSinchlist)    {Response.Write (s); }}

Obviously, if you change the Server.Transfer to Response.Redirect, the data in the HttpContext.Current.Items will not be available in the new page because it is a different HTTP request. This will report System.NullReferenceException: The object reference is not set to an instance of the object.

Information:

What does the "state" in the stateless State of the HTTP protocol mean?! http://www.cnblogs.com/bellkosmos/p/5237146.html

About the Items property of HttpContext http://www.jcwcn.com/article-14459-1.html

Use of HttpContext.Current.Items

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.