WinRT: Obtain the parameter (QueryString) Key-value pair in the Uri.
In the console or other types of projects, you can add System. Web and use the following code to obtain parameters in a Uri:
Uri uri = new Uri("http://www.cnblogs.com/h82258652/?gender=male&age=17");NameValueCollection collection = HttpUtility.ParseQueryString(uri.Query);for (int index = 0; index < collection.Count; index++){ var name = collection.Keys[index]; var value = collection[index];}
Then gender and age can be obtained respectively.
PS: NameValueCollection class has Keys and AllKeys two relatively easy to confuse attributes, stackoverflow has explained: http://stackoverflow.com/questions/803484/what-is-the-difference-between-the-properties-keys-and-allkeys-on-a-namevaluecol
However, the above Code cannot be used in WinRT, because the HttpUtility class does not exist in WinRT. You can also Split it by yourself, but you are worried about writing errors and Url Decoding. If the wheel can be used, there is no need to invent another wheel. So Google will find it and write a blog record.
You need to use the WwwFormUrlDecoder class. The Code is as follows:
Uri uri = new Uri ("http://www.cnblogs.com/h82258652? Gender = male & age = 17 "); WwwFormUrlDecoder decoder = new WwwFormUrlDecoder (uri. Query); // traverse. Foreach (var nameValue in decoder) {var name = nameValue. Name; var value = nameValue. Value;} // obtain a value. Var gender = decoder. GetFirstValueByName ("gender ");