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

前端 未结 30 2955
借酒劲吻你
借酒劲吻你 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:30

    This is the identical to the accepted answer except slightly more compact:

    private string ToQueryString(NameValueCollection nvc)
    {
        return "?" + string.Join("&", nvc.AllKeys.Select(k => string.Format("{0}={1}", 
            HttpUtility.UrlEncode(k), 
            HttpUtility.UrlEncode(nvc[k]))));
    }
    

提交回复
热议问题