How to build a query string for a URL in C#?

前端 未结 30 2510
借酒劲吻你
借酒劲吻你 2020-11-22 01:55

A common task when calling web resources from a code is building a query string to including all the necessary parameters. While by all means no rocket science, there are so

30条回答
  •  情深已故
    2020-11-22 02:44

        public static string ToQueryString(this Dictionary source)
        {
            return String.Join("&", source.Select(kvp => String.Format("{0}={1}", HttpUtility.UrlEncode(kvp.Key), HttpUtility.UrlEncode(kvp.Value))).ToArray());
        }
    
        public static string ToQueryString(this NameValueCollection source)
        {
            return String.Join("&", source.Cast().Select(key => String.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(source[key]))).ToArray());
        }
    

提交回复
热议问题