HttpContext.Current.Items vs HttpContext.Current.Session in ASP
The HttpContext class in ASP.NET encapsulates the current HTTPS request details related to HTTPS. The Current property of this class handles the HttpContext object sent in an HTTPS request.
There are two sub-properties of the Current property that store data differently:
Items
Session
These properties are responsible for the state management of the website. However, there are some critical differences in the way they store data.
Syntax
We can use the Items property:
HttpContext.Current.Items[<label>] = value;
We can use the Session property:
HttpContext.Current.Session[<label>] = value;
The above code blocks show that the two properties have similar syntax. The <label> is replaced with any string that will act as a label for the data we store in an Items or Session property.
Differences
The following table represents how these two properties differ:
Items | Session |
Data is cleared after the HTTP request is catered. | Data is cleared only when the browser is closed. |
It is useful for maintaining data for a single web page. | It is useful for maintaining data over all the web pages of a website. |
Common uses
The Items property may be used to store data related to a function, item details for a catalog, quantity information of a product, and more.
The Session property may be used to store data related to user credentials, shopping cart information, user preferences, and more.
Free Resources