Home » ASP.NET » HttpContext.Items[] vs Session[]

HttpContext.Items[] vs Session[]

ETHN0171

Since .NET first became available, passing data around during a request has become a lot easier.  The ability to set a property has made that so.  Still, there are times when setting a property just won’t do the trick.

One such time is getting data from the middle tier back up to the view separate from a DataBinding operation.  That is, you databind a control to the middle tier and that method needs to set a value that will be used elsewhere in the view, not in the item that is being bound.

The natural, obvious tendency is to set a session variable.  But there is a better way.

The problem with session variables is that they have to be cleaned up manually or they will hang around longer than we actually need them.  This will use up more session memory than is required and can potentially cause side effects that will be difficult to debug.

Instead you can use the Items[] collection that is part of the HttpContext class.  It works the same as a session variable, but it only hangs around for the duration of the request.  Once the information is sent back to the browser, the variables that were set in the Items[] collection go away.

You might set your variable in the middle tier like this:

HttpContext.Current.Items["myVar"] = "Some Data Here";

And retrieve it later like this:

string myVar = (string)(HttpContext.Current.Items["myVar"]);

 

Other post in ASP.NET

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS. Does your team need additional help in any of the above? Contact Dave today.

Leave a Reply

2 Comments on "HttpContext.Items[] vs Session[]"

Notify of
avatar
Sort by:   newest | oldest | most voted
Mahmoud
Guest

I think it will be better if you use the ViewState object,in case you want to work in the page level.
The advantages of it, the value of the object exists while Post Backs.

Mitch
Guest

@Mahmoud

Did you even read the article?

Tell me how using ViewState will allow me to pass variables around during the request.

wpDiscuz