比如已知:
https://q.cnblogs.com/list
想得到的结果是:
https://q.cnblogs.com/list?key1=value1&key2=value2
我的实现方法:
1)基于.NET Framework
var query = HttpUtility.ParseQueryString(string.Empty);
query["key1"] = "value1";
query["key2"] = "value2";
var queryString = query.ToString();
var uri = "https://q.cnblogs.com/list" + (string.IsNullOrEmpty(queryString) ? "" : "?") + queryString;
2)基于.NET Core
var queryString = new QueryString();
queryString += QueryString.Create("key1", "value1");
queryString += QueryString.Create("key2", "value2");
var uri = "https://q.cnblogs.com/list" + queryString.ToUriComponent();
QueryString类是.NET Core中新增的,在Microsoft.AspNetCore.Http.Abstractions程序集中
第一种QueryString是NameValueCollection类型,感觉和Dictionary差不多,可能内部重写了ToString,猜的~
Core里面QueryString可以用+=倒是蛮有意思的
@balahoho: NameValueCollection会进行UrlEncode
用字典啊,然后拼接url
我习惯用linq..select后jion