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:
- string querystring = "?var1=test&var2=test2"
- NameValueCollection nvColl = HttpUtility.ParseQueryString(querystring);
- string var1 = nvColl["var1"];
- string var2 = nvColl["var2"];
The string "var1" contains "test" and the "var2" contains "test2".