3 hot uses for HttpContext.Current.Items “They” won’t tell you about.

It’s been a while since I rapped at ya, so I figured I’d write a little about one of my good friends, HttpContext.Current.Items.

Generally, HttpContext.Current.Items doesn’t get all that much hot blog press, but let me tell you, I’m here to change all that. For those out of the know, System.Web.HttpContext.Current.Items is a sweet key-value pair collection you can use to pass objects around up and through all components that participate in a single HTTP request. What does this mean? This means that in a way similar to sticking something in the Session or cache, you can jam some values into the HttpContext.Current.Items for your request, say in a HttpModule way down low before you’ve even begun to fetch a page, and have those values readable/writable later from your page and all its usercontrols. The Items only persist through this one-night-stand of a single request and then supposedly “lose your number” but that’s ok, because we don’t really need all their drama after that anyway.

1) Preparing objects down low, on the down-low.

As alluded to earlier, one sweet use of HCI (as we call in on the street) is in HttpModules. Let’s say you are doing some url-rewriting for user-friendliness or SEO reasons. While you’re at it, why not do a little preparation for your page? If you have a query param of state abbreviation that comes in commonly, populate an additional full state name display field ahead of time. Clean up your strings with proper casing, do whatever utils you think you can get away with while you have the request in your hand. I have a little object of getters and setters that I populate in the the HttpModule, and stick it in the Current.Items for its way up the stack. Now my pages and usercontrols can pull out my cleaned up custom object from the Context.Items and act accordingly, pass it down the line to methods, whatever. The vibe is maybe a hint at a smidgen of a wee bit Model View Controller-ish, but not really.

2) Making params and pages more unit-testable.

With something like #1 in place, you can pull objects of params and prepared goodness out of the request and process them in your code behind, presenter, usercontrol, whatever strikes your pattern fancy. If this bundle of params is a in a nice little object that implements an interface, this makes unit testing logic that under normal circumstances relies upon getting info from the System.Web namespace (querystring params mostly) nice and easily decoupled. Pros of this are that your view calling your presenter can stay super lean and mean without having to populate a bunch info from query strings which will end up going through standard transformations you could have handled earlier on. A con is that the population of these params might be a bit mysterious to other devs who don’t see the HttpModule in action, sort of a like a table that gets populated from somewhere or other, but you don’t know what trigger where. I hate that.

3) Populating usercontrols without the hassle.

If you know you have values in your hand at page on_load or earlier, it’s pretty damn convenient to stick them in the HttpContext.Current.Items and then just read them out from whatever usercontrols may or may not be dynamically included on the page. No finding child controls from the page, no finding parent info from the controls. No casting, scoping or otherwise thinking about precisely what order what will fire. If you have the data at page_load, your controls can get it. Don’t call me, and I won’t call you either. Ta-dow (does anyone say that anymore?).

So there you have it, HttpContext.Current.Items, arcane enough to give you guru-cred to the mid-range noobs, simple enough that it can be leveraged for good and / or evil. Awesome.

HttpContext.Current.Items is awesome.

kick it on DotNetKicks.com

12 comments ↓

#1 Wöchentliche Rundablage: Acropolis, Astoria, ASP.NET MCV, WPF / Silverlight, OpenSocial, Surface… | Code-Inside Blog on 11.05.07 at 1:39 pm

[...] HttpContext und alles was man damit anstellen kann. [...]

#2 brad dunbar on 11.05.07 at 5:02 pm

Great post. I been looking to get some cred around here. (And no, no one says Tadow anymore, but I’m considering bringing it back now.)

#3 another on 11.06.07 at 2:11 am

Another good use of it is to keep db connection handles alive during an HttpRequest lifetime. I have used this approach, along with UrlRewriting, and HttpContext.Current.Items has saved my ass many times.

#4 Finds of the Week - Nov 2, 2007 » Chinh Do on 11.07.07 at 11:41 pm

[...] Duncan wrote a nice article on 3 hot uses for HttpContext.Current.Items “They” won’t tell you about. Wish I had thought about this 6 months ago when I was trying to implement a "static" [...]

#5 kevin Mocha - 3 hot uses for HttpContext.Current.Items “They” won’t tell you about. on 11.08.07 at 2:24 pm

[...] From: http://www.mikeduncan.com/3-hot-uses-for-httpcontextcurrentitems-they-wont-tell-you-about/ [...]

#6 runnane » Blog Archive » HttpContext.Current.Items on 03.05.08 at 2:55 am

[...] http://www.mikeduncan.com/3-hot-uses-for-httpcontextcurrentitems-they-wont-tell-you-about [...]

#7 [tourist].Tam on 03.17.08 at 12:25 pm

instead of regexbuddy give a try to expresso: http://www.ultrapico.com/Expresso.htm

#8 Darian Mccormick on 03.24.08 at 9:55 am

unqualitied horatius shotten golee blame sprinklered inalterability indigenal
UCD Judo Club
http://www.lorienengineering.com/

#9 James on 04.09.08 at 9:12 am

Hope you can answer this question… What are the performance issues if the object you save here is pretty complex? I am talking about an object containing other objects that contain still more objects. Is this a major performance issue?

#10 AP on 10.21.09 at 6:13 pm

Nice blog. A little humour never hurt anyone, its educational and fun.

#11 Dormand on 01.13.10 at 8:50 am

Need example code with article. then it is perfect.

#12 Suhash on 01.25.10 at 8:39 am

Great stuff.. I used in one of my projects. Very useful.

Leave a Comment