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

Related Post

  • Multi-Step Processing in ASP.NETMulti-Step Processing in ASP.NET I received the following question a few days ago but I’ve been so busy with billable work that I just haven’t had a chance to answer it until now.  Actually, I’m still busy, but I […]
  • Tracking Down Performance Issues in ASP.NETTracking Down Performance Issues in ASP.NET Last night, one of my clients assigned me a problem that I thought was going to require one solution, and in the end it was just poor programming. But the process reminded me of the need […]
  • iTextSharp – HTML to PDF – Cleaning HTMLiTextSharp – HTML to PDF – Cleaning HTML The last prerequisite step prior to actually converting our HTML into PDF code is to clean up the HTML. The method I use takes advantage of the XML parser in .NET but in order to […]
  • Prevent Postback on ButtonsPrevent Postback on Buttons Over the weekend I got a question about how to prevent postbacks on buttons from within jQuery tabs.  But the question really isn’t specific to jQuery.  There are other times when you […]
  • ASP.NET Dynamic ValidatorASP.NET Dynamic Validator One of the controls that was added to ASP.NET 3.5 in the SP1 release was the Dynamic Validator control. I completely missed it. What it does is pretty cool.  But it doesn’t […]

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.

  • Mahmoud

    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

    @Mahmoud

    Did you even read the article?

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