Difference Between HttpContext.Current.Items and HttpContext.Current.Session in ASP.Net

Session state is one of the popular state management techniques in ASP.NET environment. We developer people play with session storage every now and then. It’s pretty simple to manage session if you understand the basic concept. Here is the syntax to do that.

Session[“KEY”] =”Value”;
Or
Session[index] =”Value”; .

That’s very fine when we are in code behind of Web Form application. But when we will implement session in service based application. (Let’s think WebMethod in code behind which must be defined as static if we want to invoke using JQuery ajax()) the syntax is bit different.

HttpContext.Current.Session[“KEY”] =”Value”;
or
HttpContext.Current.Session[index] =”Value”;

This small discussion is enough to understand the syntax to deal with session variable in C#/ASP.NET. Let’s come to our main discussion. What is the difference between HttpContext.Current.Session and HttpContext.current.Item.

If it is question in interview then the single line answer is “ HttpContext.Current.Item” data is live for single HTTP request/Response where HttpContext.Current.Session data is live throughout user’s session.

So, HttpContext.Current.Item is short term storage. Means ,data store in this variable for shortest time period. Items collection in HttpContext is an IDictionary key-value collection and that are shared across single HTTP request.

Once the server finish the processing of data which is stored in Items[] and send back result in browser, the data flush automatically(yes, without any external event).

Here is syntax to save data in Items[] collection.

HttpContext.Current.Items["Value"] = "This is value";

And in case of data retrieval, we can use below syntax.

HttpContext.Current.Items["Value"]

But, in case of Session[] collection the data store during current session of user.

Let’s implement the concept in example. Here we will compare both Session[] collection with Items[] collection.

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.UI;    
  6. using System.Web.UI.WebControls;    
  7. namespace WebApp    
  8. {    
  9.     public partial class WebForm1 : System.Web.UI.Page    
  10.     {    
  11.         protected void Page_Load(object sender, EventArgs e)    
  12.         {    
  13.             if (!IsPostBack)    
  14.             {    
  15.                 HttpContext.Current.Items["Value"] = "Sourav Kayal in ITEM";    
  16.                 HttpContext.Current.Session["Value"] = "Sourav Kayal in SESSION";    
  17.                 Response.Write((string)(HttpContext.Current.Items["Value"]) + "<br>");    
  18.                 Response.Write((string)(HttpContext.Current.Session["Value"]));    
  19.             }    
  20.         }    
  21.         protected void Button1_Click(object sender, EventArgs e)    
  22.         {    
  23.             Response.Write((string)(HttpContext.Current.Items["Value"]) + "<br>");    
  24.             Response.Write((string)(HttpContext.Current.Session["Value"]));    
  25.         }    
  26.     }    
  27. } 

The trick, which we have adopted in this example, is very simple. In page loading time we are checking whether the page is loading at first time or not? If the page loads at first time we are initializing both Session[] and Items[] collection and we are printing both data in next two lines. This gives below output.

Session collection with Items collection

So, in time of collection initialization both session and Items are active and we are getting the value what we have set it.

Now, n second run we will press the button which we have placed in page. The trick is that, after button pressing the page will get post back but the content of within if condition in Page_Load() method will not execute. Because the page is loading by post back . So, the session and Items will not initialize in second time.

Then control will execute the click event of button, where we are trying to print value of both Session and Items collection by below code.
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     Response.Write((string)(HttpContext.Current.Items["Value"]) + "<br>");  
  4.     Response.Write((string)(HttpContext.Current.Session["Value"]));  
  5. } 

And we are getting value only from Session collection not from Items collection. The session is at the time of page load( first load) the Items collection was populated and the result came back in browser and after that it got flash where session data is still alive in post back operation.

Items collection

Fine, we have understood the basic difference between Session and Collection in ASP.NET. Now, the question is in which scenario Items[] collection is best to use ?

As Items[] life is within single HTTP request, we cannot use it to store user’s data in subsequent request.

One of the best places to use Items collection is to pass data from between HttpModule and HttpHandler. As we know that each and every HTTP request passes through HttpModule and HttpHandler to make communication between then we can use Items collection and when the HTTP request will pass through, the Items collection will flash automatically.

The another possible place to use Items collection when you are sharing same information across the different instance based on the user request and the request could be change for different request.

So, those are the possible situation where Items collection comes in picture. The fact should keep in mind that, Items store data as object so proper type casting is needed to get actual result from Items collection.

Summary

The main difference between Session and Items is it’s lifetime. I hope session is not now to you and you know the life of session variable pretty well. The life of Items collection is very short and it only cover single HTTP request.


Similar Articles