Sunday, May 1, 2011

Best way to convert query string to dictionary in C#

Hi,

I'm looking for the simplest way of converting a query string from an HTTP GET request into a Dictionary, and back again.

I figure it's easier to carry out various manipulations on the query once it is in dictionary form, but I seem to have a lot of code just to do the conversion. Any recommended ways?

From stackoverflow
  • How about HttpUtility.ParseQueryString?

    Just add a reference to System.Web.dll

  • HttpUtility.ParseQueryString() parses query string into a NameValueCollection object, converting the latter to an IDictionary<string, string> is a matter of a simple foreach. This, however, might be unnecessary since NameValueCollection has an indexer, so it behaves pretty much like a dictionary.

0 comments:

Post a Comment