Using HttpRequest.Item property

I’m currently in the last part of my military service and don’t have much time to work or blog. However I could answer some Stack Overflow questions. One of them was the question if it is a good practice to use the HttpRequest.Item property instead of HttpRequest.Form or HttpRequest.QueryString.

When you want to get a query string value, you can get this from the QueryString property of the current http request object. If you want to get a posted form value, via the Form collection.

// query string
HttpContext.Current.Request.QueryString["param"];

// form values
HttpContext.Current.Request.Form["param"];

Exactly the same is available for Cookies and ServerVariables. There is also the HttpRequest.Item property, but what is it for?

// what is the result of this?
HttpContext.Current.Request["param"];

The MSDN doesn’t help you finding what exactly is going on there. After a bit of research I found that the value will be calculated in the following order:

  1. Value from HttpRequest.QueryString if not null
  2. Value from HttpRequest.Form if not null
  3. Value from HttpRequest.Cookies if not null
  4. Value from HttpRequest.ServerVariables if not null
  5. If no value was found, null

Simple hm? But is this a good practice to use? I think no. I wonder if your application is well designed if you don’t know from which collection you expect a value? I also think it could be a security risk if you don’t handle all side effects of this. And last but not least, can you really maintain this 1 or 2 years after you wrote this code? So better use the collection where you expect the value coming from.

Kevin Brechbühl

Senior Developer & Architect | C# & ASP.NET | Sitecore Technology MVP

comments powered by Disqus