Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Parse QueryString with the HttpUtility

If you have a querystring from for example the Uri class (Uri.Query) and you want to parse it so you can get al the params and values, instead of doing al lot of string manipulation you can use a very handy utility class, the HttpUtility. The HttpUtility class has a method ParseQueryString, this method parse a query string into a namevaluecollection. Like this:

  1. string querystring = "?var1=test&var2=test2"
  2.  
  3. NameValueCollection nvColl = HttpUtility.ParseQueryString(querystring);
  4.  
  5. string var1 = nvColl["var1"];
  6. string var2 = nvColl["var2"];

 The string "var1" contains "test" and the "var2" contains "test2".

4 Comments

Comments have been disabled for this content.