星空网 > 软件开发 > ASP.net

【WinRT】获取 Uri 中的参数(QueryString)键值对

在控制台或者其它类型的项目中,可以添加 System.Web,使用以下代码来获取一个 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];}

那么就可以分别得到 gender 和 age。

PS:NameValueCollection 类有 Keys 和 AllKeys 两个比较容易令人糊涂的属性,stackoverflow 上有讲解:http://stackoverflow.com/questions/803484/what-is-the-difference-between-the-properties-keys-and-allkeys-on-a-namevaluecol

 

但是,上面的代码在 WinRT 中没法用了,因为 HttpUtility 这个类在 WinRT 中是不存在的。自己 Split 获取吧,也行,不过就是担心写错,还有 Url 解码那东西呢。能有轮子用的话,就没必要再去发明个轮子了,于是谷歌一下,终于找到了,于是就写篇 blog 记录下。

需要使用 WwwFormUrlDecoder 这个类。代码如下:

Uri uri = new Uri("http://www.cnblogs.com/h82258652/?gender=male&age=17");WwwFormUrlDecoder decoder = new WwwFormUrlDecoder(uri.Query);// 遍历。foreach (var nameValue in decoder){  var name = nameValue.Name;  var value = nameValue.Value;}// 获取某一个的值。var gender = decoder.GetFirstValueByName("gender");




原标题:【WinRT】获取 Uri 中的参数(QueryString)键值对

关键词:string

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

蚂蚁刊登软件:https://www.ikjzd.com/w/1573202581527109634
美速达:https://www.ikjzd.com/w/1573202582097534978
美匣云ERP:https://www.ikjzd.com/w/1573202582613434370
美赞拓:https://www.ikjzd.com/w/1573202583431323650
McAfee SECURE:https://www.ikjzd.com/w/1573202584035303426
Multi-Channel Fulfillment(多渠道配送):https://www.ikjzd.com/w/1573202585382019073
2022世界杯门票如何买?:https://www.vstour.cn/a/363182.html
沈阳到本溪的距离有多远?(详细路线及交通方式):https://www.vstour.cn/a/363183.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流