WinRT: Obtain the parameter (QueryString) Key-value pair in the Uri.

Source: Internet
Author: User

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 ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.