36

Is there a way to convert a Dictionary in code into a url parameter string?

e.g.

// An example list of parameters
Dictionary<string, object> parameters ...;
foreach (Item in List)
{
    parameters.Add(Item.Name, Item.Value);
}

string url = "http://www.somesite.com?" + parameters.XX.ToString();

Inside MVC HtmlHelpers you can generate URLs with the UrlHelper (or Url in controllers) but in Web Forms code-behind the this HtmlHelper is not available.

string url = UrlHelper.GenerateUrl("Default", "Action", "Controller", 
    new RouteValueDictionary(parameters), htmlHelper.RouteCollection , 
    htmlHelper.ViewContext.RequestContext, true);

How could this be done in C# Web Forms code-behind (in an MVC/Web Forms app) without the MVC helper?

0

13 Answers 13

67

One approach would be:

var url = string.Format("https://example.org?{0}",
    string.Join("&",
        parameters.Select(kvp =>
            string.Format(
                "{0}={1}",
                kvp.Key,
                HttpUtility.UrlEncode(kvp.Value.ToString())))));

You could also use string interpolation as introduced in C#6:

var url = $"https://example.org?{string.Join("&", parameters.Select(kvp => $"{kvp.Key}={HttpUtility.UrlEncode(kvp.Value.ToString())}"))}";

And you could get rid of the UrlEncode if you don't need it, I just added it for completeness.

1
  • Not such efficient: string.Format repeated (params.Count + 1) times, creating IEnumerator<T> object, so many methods are invoked. Creating anonymous method. So much work to simply concatenate strings (.
    – Stas BZ
    Commented May 30, 2016 at 9:43
36

You can use QueryHelpers from Microsoft.AspNetCore.WebUtilities:

string url = QueryHelpers.AddQueryString("https://me.com/xxx.js", dictionary);
3
  • 2
    This should be the accepted answer (at least as of .NET Core 3.1+). AddQueryString has an overload that takes a Dictionary that lets you append query keys/values to an existing string URL - perfect.
    – qJake
    Commented May 24, 2022 at 21:35
  • @PeterRiesz Proof? Source does not show [Obsolete].
    – lonix
    Commented Apr 13 at 1:03
  • I can't remember why I commenteded that. It was year ago, I must have mixed up the deprection warning with something else. Commented Apr 14 at 2:47
17

I'm not saying this option is better (I personally think it's not), but I'm here just to say it exists.

The QueryBuilder class:

var queryStringDictionary = new Dictionary<string, string>
{
    { "username", "foo" },
    { "password", "bar" }
};

var queryBuilder = new QueryBuilder(queryStringDictionary);
queryBuilder.Add("type", "user");

//?username=foo&password=bar&type=user
QueryString result = queryBuilder.ToQueryString();
1
  • 3
    Why do you think it's not the better option? I think it's better because you don't have to encode the values yourself. It's a much smaller solution.
    – Ryan Allen
    Commented Mar 17, 2021 at 3:40
14

Make a static helper class perhaps:

public static string QueryString(IDictionary<string, object> dict)
{
    var list = new List<string>();
    foreach(var item in dict)
    {
        list.Add(item.Key + "=" + item.Value);
    }
    return string.Join("&", list);
}
5

the most short way:

string s = string.Join("&", dd.Select((x) => x.Key + "=" + x.Value.ToString()));

But shorter does not mean more efficiency. Better use StringBuilder and Append method:

first = true;
foreach(var item in dd)
{
    if (first)
        first = false;
    else
        sb.Append('&');
    sb.Append(item.Key);
    sb.Append('=');
    sb.Append(item.Value.ToString());
}
1
5

You could use a IEnumerable<string> and String.Join:

var parameters = new List<string>();
foreach (var item in List)
{
    parameters.Add(item.Name + "=" + item.Value.ToString());
}

string url = "http://www.somesite.com?" + String.Join("&", parameters);

or shorter

string baseUri = "http://www.somesite.com?";
string url = baseUri + String.Join("&", list.Select(i => $"{i.Name}={i.Value}"));
1
  • I'm clearly missing something, because it looks like if you pass in { "abc" -> "def", "ghi" -> "jkl" } this produces abc=&def&ghi=&jkl.
    – anon
    Commented Nov 10, 2016 at 5:19
3
// this is wrong
var url = string.Format("http://www.yoursite.com?{0}",
    HttpUtility.UrlEncode(string.Join("&",
        parameters.Select(kvp =>
            string.Format("{0}={1}", kvp.Key, kvp.Value))))); 

I tried this code from the accepted answer (of Mike), but it did not worked. Actually that is wrong. Let's see what is wrong with that answer. If you write HttpUtility.UrlEncode("a=1&b=2"), it will also encode = and & and returns "a%3d1%26b%3d2" which is not correct. So, you need to encode just keys and values like this:

// this works
var encodedUrl = string.Format("http://www.yoursite.com?{0}", 
    string.Join("&", parameters.Select(kvp => string.Format("{0}={1}", 
        HttpUtility.UrlEncode(kvp.Key), HttpUtility.UrlEncode(kvp.Value)))));

Or (using string interpolation)

var encodedUrl = $"http://www.yoursite.com?{string.Join("&", parameters.Select(kvp => $"{HttpUtility.UrlEncode(kvp.Key)}={HttpUtility.UrlEncode(kvp.Value)}"))}";
2

I have written these extensions methods:

Add querystring to base url:

public static string AddQueryString(this string url, IDictionary<string, object> parameters) => 
     $"{url}?{parameters.ToQueryString()}";

Convert dictionary of parameters to query string:

private static string ToQueryString(this IDictionary<string, object> parameters) => 
    string.Join("&", parameters.Select(x => $"{x.Key}={x.Value}"));

Actual code to convert to query string:

string.Join("&", parameters.Select(x => $"{x.Key}={x.Value}"));

Edit:

When using in production consider encoding the parameters with URL.Encode within this function. If you use this make sure the parameters in the dictionary are not already encoded.

4
  • @CarlosMuñoz implementation detail
    – Nick N.
    Commented May 30, 2018 at 8:46
  • 3
    Yes, correct implementation detail. Otherwise It could lead to bugs Commented May 30, 2018 at 13:14
  • Agree with Carlos: string.Join("&", parameters.Select(kvp => $"{WebUtility.UrlEncode(kvp.Key)}={WebUtility.UrlEncode(kvp.Value.ToString())}"));
    – Thymine
    Commented Jul 5, 2018 at 22:58
  • @Thymine I added a comment. In my scenario, the parameters are already encoded within the dictionary.
    – Nick N.
    Commented Jan 25, 2021 at 14:13
1

Is this what you looking for (untested code)?

StringBuilder sb = new StringBuilder();
sb.Append("http://www.somesite.com?");

foreach(var item in parameters)
{
sb.append(string.Format("{0}={1}&", item.Key, item.Value))
}
string finalUrl = sb.ToString();
finalUrl = finalUrl.Remove(finalUrl.LasIndexOf("&"));
1

You may try this:

var parameters = new Dictionary<string, string>(); // You pass this
var url = "http://www.somesite.com?";
int i = 0;
foreach (var item in parameters)
{
    url += item.Key + "=" + item.Value;
    url += i != parameters.Count ? "&" : string.Empty;
    i++;
}

return url;

I have not run the logic, but this might help you.

If you would be UrlRouting in webforms then it would be a different story.

Check out:

http://msdn.microsoft.com/en-us/library/cc668201(v=vs.90).aspx

0

Using Newtonsoft JSON you can can quickly serialize an anonymous object into a http query by making extension functions.

This method also supports passing arrays as parameters to PHP using brackets.

    public static string Join(this List<string> a, string seperator)
    {
        return string.Join(seperator, a.ToArray());
    }

    public static string ToHttpQuery(this object a)
    {
        string result = "";

        foreach (var b in JObject.FromObject(a))
        {
            if (result.Length > 0)
                result += "&";

            if (b.Value.Type == JTokenType.Array)
                result += b.Value.Select(x => b.Key + "[]=" + Uri.EscapeDataString(x.ToString())).ToList().Join("&");
            else
                result += b.Key + "=" + Uri.EscapeDataString(b.Value.ToString());
        }

        return result;
    }

Example usage:

        string query = new
        {
            test = new string[] { "ree", "roo" },
            myId = 123,
            port = PORTS.MAIN_INTERFACE_IO,
            targetUrl = "http://example.com/?fee=123"
        }.ToHttpQuery();
        
        string myUrl = "http://example.com/?" + query;
0

This is old also there are other correct answers.

However, the builtin HttpValueCollection already does escaping correctly, the only trick then is to rewrite the dictionary to it.

This means the following snippet reuses the implementation from the base class library and would be useful for anyone not using the NET.Core implementations (for example - people who still code against NET.Framework)

public static string ToQueryString( IDictionary<string, object> dict )
{
    var collection = HttpUtility.ParseQueryString(string.Empty);

    foreach ( var key in dict.Keys )
    {
        collection.Add( key, dict[key].ToString() );
    }

    return collection.ToString();
}
-1

You can add the following class to the project and use the extension method.

using System.Collections.Generic;
using System.Linq;
using System.Text;

public static class CollectionExtensions {
    public static string ToQueryString(this IDictionary<string, string> dict)
    {

    if (dict.Count == 0) return string.Empty;

    var buffer = new StringBuilder();
    int count = 0;
    bool end = false;

    foreach (var key in dict.Keys)
    {
        if (count == dict.Count - 1) end = true;

        if (end)
            buffer.AppendFormat("{0}={1}", key, dict[key]);
        else
            buffer.AppendFormat("{0}={1}&", key, dict[key]);

        count++;
    }

    return buffer.ToString();
}
}

to use the code:

var queryString = dictionary.ToQueryString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.