21

I have a URL that also might have a query string part, the query string might be empty or have multiple items.

I want to replace one of the items in the query string or add it if the item doesn't already exists.

I have an URI object with the complete URL.

My first idea was to use regex and some string magic, that should do it.

But it seems a bit shaky, perhaps the framework has some query string builder class?

10 Answers 10

51

I found this was a more elegant solution

var qs = HttpUtility.ParseQueryString(Request.QueryString.ToString());
qs.Set("item", newItemValue);
Console.WriteLine(qs.ToString());
3
  • Most concise answer. Thanks!
    – Neil
    Nov 14, 2015 at 19:41
  • 1
    Note that this works because ParseQueryString actually returns an instance of HttpValueCollection, which overrides object.ToString, while a normal NameValueCollection object would just print out System.Collections.Specialized.NameValueCollection.
    – Timothy
    Oct 25, 2017 at 18:26
  • This should be the accepted answer. Also, I confirmed that the name parameter in qs.Set() is case-insensitive.
    – drewmerk
    May 19, 2021 at 16:41
35

Lets have this url: https://localhost/video?param1=value1

At first update specific query string param to new value:

var uri = new Uri("https://localhost/video?param1=value1");
var qs = HttpUtility.ParseQueryString(uri.Query);
qs.Set("param1", "newValue2");

Next create UriBuilder and update Query property to produce new uri with changed param value.

var uriBuilder = new UriBuilder(uri);
uriBuilder.Query = qs.ToString();
var newUri = uriBuilder.Uri;

Now you have in newUri this value: https://localhost/video?param1=newValue2

1
  • 5
    No need to create a separate Uri object. We can do as follows: var uriBuilder = new UriBuilder("https://localhost/video?param1=value1"); var qs = HttpUtility.ParseQueryString(uriBuilder.Query); qs.Set("param1", "newValue2"); uriBuilder.Query = qs.ToString(); var newUri = uriBuilder.Uri; Oct 20, 2016 at 6:31
5

I use following method:

    public static string replaceQueryString(System.Web.HttpRequest request, string key, string value)
    {
        System.Collections.Specialized.NameValueCollection t = HttpUtility.ParseQueryString(request.Url.Query);
        t.Set(key, value);
        return t.ToString();
    }
1
  • Love this solution. And if the original url is a string, we can just convert it to a uri and use it like this: public static string ReplaceQueryString(string url, string key, string value) { var uri = new UriBuilder(url); var t = HttpUtility.ParseQueryString(uri.Query); ... Mar 27, 2014 at 15:29
4

Maybe you could use the System.UriBuilder class. It has a Query property.

5
  • 5
    This doesn't seem to be useful. The URI object Karsten already has has the same information in it's Query property. I'm not sure why so many people voted this up or why it's the accepted answer. Am I missing something?
    – Ed Bishop
    Mar 20, 2014 at 10:57
  • 1
    The Query property on the System.UriBuilder class is mutable.
    – knut
    Apr 25, 2014 at 9:50
  • 2
    This is not useful, the Query property is just a string
    – Alex
    Apr 8, 2016 at 11:28
  • 3
    stackoverflow.com/questions/14517798/… this answer is better.
    – Alex Zhang
    Oct 9, 2018 at 6:49
  • 2
    This is a non-answer.
    – Stilgar
    Dec 28, 2019 at 19:29
4
string link = page.Request.Url.ToString();

if(page.Request.Url.Query == "")
    link  += "?pageIndex=" + pageIndex;
else if (page.Request.QueryString["pageIndex"] != "")
{
    var idx = page.Request.QueryString["pageIndex"];
    link = link.Replace("pageIndex=" + idx, "pageIndex=" + pageIndex);
}
else 
    link += "&pageIndex=" + pageIndex;

This seems to work really well.

3

No, the framework doesn't have any existing QueryStringBuilder class, but usually the querystring information in a HTTP request is available as an iterable and searchable NameValueCollection via the Request.Querystring property.

Since you are starting off with a Uri object, however, you will need to obtain the querystring portion using the Query property of the Uri object. This will yield a string of the form:

Uri myURI = new Uri("http://www.mywebsite.com/page.aspx?Val1=A&Val2=B&Val3=C");
string querystring = myURI.Query;

// Outputs: "?Val1=A&Val2=B&Val3=C". Note the ? prefix!
Console.WriteLine(querystring);

You can then split this string on the ampersand character to differentiate it into different querystring parameters-value pairs. Then again split each parameter on the "=" character to differentiate it into a key and value.

Since your final goal is to search for a particular querystring key and if necessary create it, you should try to (re)create a collection (preferably, a generic one) that allows you easily search in the collection, similar to the facility provided by the NameValueCollection class.

0
2

I used the following code to append/replace the value of a parameter in the current request URL:

    public static string CurrentUrlWithParam(this UrlHelper helper, string paramName, string paramValue)
    {
        var url = helper.RequestContext.HttpContext.Request.Url;
        var sb = new StringBuilder();

        sb.AppendFormat("{0}://{1}{2}{3}",
                        url.Scheme,
                        url.Host,
                        url.IsDefaultPort ? "" : ":" + url.Port,
                        url.LocalPath);

        var isFirst = true;

        if (!String.IsNullOrWhiteSpace(url.Query))
        {
            var queryStrings = url.Query.Split(new[] { '?', ';' });
            foreach (var queryString in queryStrings)
            {
                if (!String.IsNullOrWhiteSpace(queryString) && !queryString.StartsWith(paramName + "="))
                {
                    sb.AppendFormat("{0}{1}", isFirst ? "?" : ";", queryString);
                    isFirst = false;
                }
            }
        }

        sb.AppendFormat("{0}{1}={2}", isFirst ? "?" : ";", paramName, paramValue);

        return sb.ToString();
    }

Maybe this helps others when finding this topic.

Update:

Just saw the hint about UriBuilder and did a second version using UriBuilder, StringBuilder and Linq:

    public static string CurrentUrlWithParam(this UrlHelper helper, string paramName, string paramValue)
    {
        var url = helper.RequestContext.HttpContext.Request.Url;
        var ub = new UriBuilder(url.Scheme, url.Host, url.Port, url.LocalPath);

        // Query string
        var sb = new StringBuilder();
        var isFirst = true;
        if (!String.IsNullOrWhiteSpace(url.Query))
        {
            var queryStrings = url.Query.Split(new[] { '?', ';' });
            foreach (var queryString in queryStrings.Where(queryString => !String.IsNullOrWhiteSpace(queryString) && !queryString.StartsWith(paramName + "=")))
            {
                sb.AppendFormat("{0}{1}", isFirst ? "" : ";", queryString);
                isFirst = false;
            }
        }
        sb.AppendFormat("{0}{1}={2}", isFirst ? "" : ";", paramName, paramValue);
        ub.Query = sb.ToString();

        return ub.ToString();
    }
1

I agree with Cerebrus. Sticking to the KISS principle, you have the querystring,

string querystring = myURI.Query; 

you know what you are looking for and what you want to replace it with.

So use something like this:-

if (querystring == "") 
  myURI.Query += "?" + replacestring; 
else 
  querystring.replace (searchstring, replacestring); // not too sure of syntax !!
0

I answered a similar question a while ago. Basically, the best way would be to use the class HttpValueCollection, which the QueryString property actually is, unfortunately it is internal in the .NET framework. You could use Reflector to grab it (and place it into your Utils class). This way you could manipulate the query string like a NameValueCollection, but with all the url encoding/decoding issues taken care for you.

HttpValueCollection extends NameValueCollection, and has a constructor that takes an encoded query string (ampersands and question marks included), and it overrides a ToString() method to later rebuild the query string from the underlying collection.

0
   public class QueryParams : Dictionary<string,string>
   {
       private Uri originolUrl;
       private Uri ammendedUrl;
       private string schemeName;
       private string hostname;
       private string path;

       public QueryParams(Uri url)
       {
           this.originolUrl = url;
           schemeName = url.Scheme;
           hostname = url.Host;
           path = url.AbsolutePath;
           //check uri to see if it has a query
           if (url.Query.Count() > 1)
           {
               //we grab the query and strip of the question mark as we do not want it attached
               string query = url.Query.TrimStart("?".ToArray());
               //we grab each query and place them into an array
               string[] parms = query.Split("&".ToArray());
               foreach (string str in parms)
               {
                   // we split each query into two strings(key) and (value) and place into array
                   string[] param = str.Split("=".ToArray());
                   //we add the strings to this dictionary
                   this.Add(param[0], param[1]);
               }
           }
       }


       public QueryParams Set(string paramName, string value)
       {

           if(this.ContainsKey(paramName))
           {
               //if key exists change value
               this[paramName] = value;
               return (this);
           }
           else
           {
               this.Add(paramName, value);
               return this;
           }
       }
       public QueryParams Set(string paramName, int value)
       {
           if (this.ContainsKey(paramName))
           {
               //if key exists change value
               this[paramName] = value.ToString();
               return (this);
           }
           else 
           {
               this.Add(paramName, value);
               return this;
           }
       }

       public void Add(string key, int value)
       {
           //overload, adds a new keypair
           string strValue = value.ToString();
           this.Add(key, strValue);
       }

       public override string ToString()
       {
           StringBuilder queryString = new StringBuilder();

           foreach (KeyValuePair<string, string> pair in this)
           {
               //we recreate the query from each keypair 
               queryString.Append(pair.Key + "=" + pair.Value + "&");
           }
           //trim the end of the query
           string modifiedQuery = queryString.ToString().TrimEnd("&".ToArray());

           if (this.Count() > 0)
           {
               UriBuilder uriBuild = new UriBuilder(schemeName, hostname);
               uriBuild.Path = path;
               uriBuild.Query = modifiedQuery;
               ammendedUrl = uriBuild.Uri;
               return ammendedUrl.AbsoluteUri;
           }
           else
           {
               return originolUrl.ToString();
           }
       }


       public Uri ToUri()
       {
          this.ToString();
          return ammendedUrl;
       }
   }
}
4
  • just wrote this code, it works for me, just wonder if the code can be improved.
    – guest
    Nov 17, 2015 at 11:59
  • Could you add some explanation to the answer?
    – Robert
    Nov 17, 2015 at 12:15
  • originol query = ?q=&geo=&distance=1&search=1&fp=1; ammended Query = ?q=10&geo=geobird&distance=10&search=8&fp=2&size=10&area=250 QueryParams pc = new QueryParams(url); pc.Add("size", 10); pc.Add("area", "250"); string ammended = pc.SetQueryParam("geo","geobird").SetQueryParam("q", "10").SetQueryParam("search", 8).SetQueryParam("fp", "2").ToString();
    – guest
    Nov 17, 2015 at 12:54
  • If you're wondering whether the code can be improved then you can post it as a question on CodeReview.SE
    – ChrisW
    Dec 16, 2015 at 11:46

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.