System.Web.HttpUtility class needs to get some appreciation!

This article was migrated from an older iteration of our website, and it could deviate in design and functionality.


I felt that I needed to give the HttpUtility some well deserved time in the spotlight. Also thought I spend some written space on the ParseQueryString() method, a really nice feature in the System.Web.HttpUtility class that helps you parse a querystring to a NameValueCollection.

Estimated read time : 5 minutes

Jump to

I felt that I needed to give the HttpUtility some well deserved time in the spotlight. Also thought I spend some written space on one really nice feature in the System.Web.HttpUtility class.

Example and why this sudden spotlight

We have built a  class that handles quite a complex query string to manage JavaScript calls. This is  a tool that a customer will be using. Reason for using the HttpRequest.QueryString to pass values is that we are using almost static files in combination with Ajax calls with JQuery. This way we can really increase cacheability in the user browser and also make the site/application easy to use.

So what happened was that it was really easy to handle the QueryString directly because the HttpRequest.QueryString returns a NameValueCollection. This object contains a collection of Name-Value pairs that are of course, the QueryString key and value properties.So this was a nice way to handle the settings and values sent backwards and forwards. The next feature that we wanted to include was  a way of saving pre-defined choices in the application. Then it was much easier to use a string to pass the the Query-parameters through to the Web Service. Since our goals is always to build applications that are scalable, easy to maintain and develop further I wanted to find a very simple way of reusing this method of parsing the querystring-formatted values.

Then there came the light and the reason for HttpUtility to get some spotlight.

The method: ParseQueryString(string query) did the trick and I could do what I wanted with almost no time at all. That’s what I like!

Short example of usage

   1: // The normal constructor that was used was
   2: public QueryStringParser(NameValueCollection pairs)
   3:         {
   4:             _pairs = pairs;
   5:         }

What we were sending in was only the Request.QueryString:

   1: // Get the QueryStringParser based on the QueryString from the Request object
   2: // The QueryString object is a NameValueCollection alread, which is nice
   3: var queryStringParser = new QueryStringParser(Request.QueryString);
   4:  
   5: // Then get the UserQuery
   6: var userQuery = queryStringParser.GetParsedInput();

What we needed was to get a constructor based on only a string. So that’s how I found the nice built in method.

   1: /// <summary>
   2:         /// Constructor that will generate the "_pairs" from a querystring-formed string.
   3:         /// The string should look like the querystring
   4:         /// </summary>
   5:         /// <param name="queryString"></param>
   6:         public QueryStringParser (string queryString)
   7:         {
   8:             if (string.IsNullOrEmpty (queryString))
   9:             {
  10:                 throw new ArgumentNullException("queryString");
  11:             }
  12:  
  13:             _pairs = HttpUtility.ParseQueryString (queryString);
  14:  
  15:         }

Other obvious reasons for using HttpUtility

HttpUtility is of course a must-use built in class in the .Net framework. Methods like UrlEncode/Decode and HtmleEncode/Decode are things that you most definitely should’ve used already. If not… You should!