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

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

    I wrote a helper for my razor project using some of the hints from other answers.

    The ParseQueryString business is necessary because we are not allowed to tamper with the QueryString object of the current request.

    @helper GetQueryStringWithValue(string key, string value) {
        var queryString = System.Web.HttpUtility.ParseQueryString(HttpContext.Current.Request.QueryString.ToString());
        queryString[key] = value;
        @Html.Raw(queryString.ToString())
    }
    

    I use it like this:

    location.search = '?@Helpers.GetQueryStringWithValue("var-name", "var-value")';
    

    If you want it to take more than one value, just change the parameters to a Dictionary and add the pairs to the query string.

    0 讨论(0)
  • 2020-11-22 02:36

    Here's a fluent/lambda-ish way as an extension method (combining concepts in previous posts) that supports multiple values for the same key. My personal preference is extensions over wrappers for discover-ability by other team members for stuff like this. Note that there's controversy around encoding methods, plenty of posts about it on Stack Overflow (one such post) and MSDN bloggers (like this one).

    public static string ToQueryString(this NameValueCollection source)
    {
        return String.Join("&", source.AllKeys
            .SelectMany(key => source.GetValues(key)
                .Select(value => String.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value))))
            .ToArray());
    }
    

    edit: with null support, though you'll probably need to adapt it for your particular situation

    public static string ToQueryString(this NameValueCollection source, bool removeEmptyEntries)
    {
        return source != null ? String.Join("&", source.AllKeys
            .Where(key => !removeEmptyEntries || source.GetValues(key)
                .Where(value => !String.IsNullOrEmpty(value))
                .Any())
            .SelectMany(key => source.GetValues(key)
                .Where(value => !removeEmptyEntries || !String.IsNullOrEmpty(value))
                .Select(value => String.Format("{0}={1}", HttpUtility.UrlEncode(key), value != null ? HttpUtility.UrlEncode(value) : string.Empty)))
            .ToArray())
            : string.Empty;
    }
    
    0 讨论(0)
  • 2020-11-22 02:36

    Add this class to your project

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    public class QueryStringBuilder
    {
        private readonly List<KeyValuePair<string, object>> _list;
    
        public QueryStringBuilder()
        {
            _list = new List<KeyValuePair<string, object>>();
        }
    
        public void Add(string name, object value)
        {
            _list.Add(new KeyValuePair<string, object>(name, value));
        }
    
        public override string ToString()
        {
            return String.Join("&", _list.Select(kvp => String.Concat(Uri.EscapeDataString(kvp.Key), "=", Uri.EscapeDataString(kvp.Value.ToString()))));
        }
    }
    

    And use it like this:

    var actual = new QueryStringBuilder {
        {"foo", 123},
        {"bar", "val31"},
        {"bar", "val32"}
    };
    
    actual.Add("a+b", "c+d");
    
    actual.ToString(); // "foo=123&bar=val31&bar=val32&a%2bb=c%2bd"
    
    0 讨论(0)
  • 2020-11-22 02:37

    Combined the top answers to create an anonymous object version:

    var queryString = HttpUtility2.BuildQueryString(new
    {
        key2 = "value2",
        key1 = "value1",
    });
    

    That generates this:

    key2=value2&key1=value1

    Here's the code:

    public static class HttpUtility2
    {
        public static string BuildQueryString<T>(T obj)
        {
            var queryString = HttpUtility.ParseQueryString(string.Empty);
    
            foreach (var property in TypeDescriptor.GetProperties(typeof(T)).Cast<PropertyDescriptor>())
            {
                var value = (property.GetValue(obj) ?? "").ToString();
                queryString.Add(property.Name, value);
            }
    
            return queryString.ToString();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:39

    How about creating extension methods that allow you to add the parameters in a fluent style like this?

    string a = "http://www.somedomain.com/somepage.html"
        .AddQueryParam("A", "TheValueOfA")
        .AddQueryParam("B", "TheValueOfB")
        .AddQueryParam("Z", "TheValueOfZ");
    
    string b = new StringBuilder("http://www.somedomain.com/anotherpage.html")
        .AddQueryParam("A", "TheValueOfA")
        .AddQueryParam("B", "TheValueOfB")
        .AddQueryParam("Z", "TheValueOfZ")
        .ToString(); 
    

    Here's the overload that uses a string:

    public static string AddQueryParam(
        this string source, string key, string value)
    {
        string delim;
        if ((source == null) || !source.Contains("?"))
        {
            delim = "?";
        }
        else if (source.EndsWith("?") || source.EndsWith("&"))
        {
            delim = string.Empty;
        }
        else
        {
            delim = "&";
        }
    
        return source + delim + HttpUtility.UrlEncode(key)
            + "=" + HttpUtility.UrlEncode(value);
    }
    

    And here's the overload that uses a StringBuilder:

    public static StringBuilder AddQueryParam(
        this StringBuilder source, string key, string value)
    {
        bool hasQuery = false;
        for (int i = 0; i < source.Length; i++)
        {
            if (source[i] == '?')
            {
                hasQuery = true;
                break;
            }
        }
    
        string delim;
        if (!hasQuery)
        {
            delim = "?";
        }
        else if ((source[source.Length - 1] == '?')
            || (source[source.Length - 1] == '&'))
        {
            delim = string.Empty;
        }
        else
        {
            delim = "&";
        }
    
        return source.Append(delim).Append(HttpUtility.UrlEncode(key))
            .Append("=").Append(HttpUtility.UrlEncode(value));
    }
    
    0 讨论(0)
  • 2020-11-22 02:40

    Here's my late entry. I didn't like any of the others for various reasons, so I wrote my own.

    This version features:

    • Use of StringBuilder only. No ToArray() calls or other extension methods. It doesn't look as pretty as some of the other responses, but I consider this a core function so efficiency is more important than having "fluent", "one-liner" code which hide inefficiencies.

    • Handles multiple values per key. (Didn't need it myself but just to silence Mauricio ;)

      public string ToQueryString(NameValueCollection nvc)
      {
          StringBuilder sb = new StringBuilder("?");
      
          bool first = true;
      
          foreach (string key in nvc.AllKeys)
          {
              foreach (string value in nvc.GetValues(key))
              {
                  if (!first)
                  {
                      sb.Append("&");
                  }
      
                  sb.AppendFormat("{0}={1}", Uri.EscapeDataString(key), Uri.EscapeDataString(value));
      
                  first = false;
              }
          }
      
          return sb.ToString();
      }
      

    Example Usage

            var queryParams = new NameValueCollection()
            {
                { "x", "1" },
                { "y", "2" },
                { "foo", "bar" },
                { "foo", "baz" },
                { "special chars", "? = &" },
            };
    
            string url = "http://example.com/stuff" + ToQueryString(queryParams);
    
            Console.WriteLine(url);
    

    Output

    http://example.com/stuff?x=1&y=2&foo=bar&foo=baz&special%20chars=%3F%20%3D%20%26
    
    0 讨论(0)
提交回复
热议问题