<p>I'm implementing a custom control and in this control I need to write a bunch of links to the current page, each one with a different query parameter. I need to keep existing query string intact, and add (or modify the value  of ) an extra query item (eg. "page"):</p>

<pre><code>"Default.aspx?page=1"
"Default.aspx?page=2"
"Default.aspx?someother=true&amp;page=2"
</code></pre>

<p>etc.</p>

<p>Is there a simple helper method that I can use  in the Render method  ... uhmm ... like:</p>

<pre><code>Page.ClientScript.SomeURLBuilderMethodHere(this,"page","1");
Page.ClientScript.SomeURLBuilderMethodHere(this,"page","2");
</code></pre>

<p>That will take care of generating a correct URL, maintain existing query string items and not create duplicates  eg. page=1&amp;page=2&amp;page=3?</p>

<p>Rolling up my own seems like such an unappealing task.</p>

[{'answer_id': 47344, 'author': 'Glenn Slaven', 'author_id': 2975, 'author_profile': 'https://Stackoverflow.com/users/2975', 'pm_score': 2, 'selected': True, 'text': '<p>I\'m afraid I don\'t know of any built-in method for this, we use this method that takes the querystring and sets parameters</p>\n\n<pre><code>    /// &lt;summary&gt;\n    /// Set a parameter value in a query string. If the parameter is not found in the passed in query string,\n    /// it is added to the end of the query string\n    /// &lt;/summary&gt;\n    /// &lt;param name="queryString"&gt;The query string that is to be manipulated&lt;/param&gt;\n    /// &lt;param name="paramName"&gt;The name of the parameter&lt;/param&gt;\n    /// &lt;param name="paramValue"&gt;The value that the parameter is to be set to&lt;/param&gt;\n    /// &lt;returns&gt;The query string with the parameter set to the new value.&lt;/returns&gt;\n    public static string SetParameter(string queryString, string paramName, object paramValue)\n    {\n        //create the regex\n        //match paramname=*\n        //string regex = String.Format(@"{0}=[^&amp;]*", paramName);\n        string regex = @"([&amp;?]{0,1})" + String.Format(@"({0}=[^&amp;]*)", paramName);\n\n        RegexOptions options = RegexOptions.RightToLeft;\n        // Querystring has parameters...\n        if (Regex.IsMatch(queryString, regex, options))\n        {\n            queryString = Regex.Replace(queryString, regex, String.Format("$1{0}={1}", paramName, paramValue));\n        }\n        else\n        {\n            // If no querystring just return the Parameter Key/Value\n            if (queryString == String.Empty)\n            {\n                return String.Format("{0}={1}", paramName, paramValue);\n            }\n            else\n            {\n                // Append the new parameter key/value to the end of querystring\n                queryString = String.Format("{0}&amp;{1}={2}", queryString, paramName, paramValue);\n            }\n        }\n        return queryString;\n    }\n</code></pre>\n\n<p>Obviously you could use the QueryString <code>NameValueCollection</code> property of the URI object to make looking up the values easier, but we wanted to be able to parse any querystring.</p>\n'}, {'answer_id': 47346, 'author': 'Glenn Slaven', 'author_id': 2975, 'author_profile': 'https://Stackoverflow.com/users/2975', 'pm_score': 0, 'selected': False, 'text': '<p>Oh and we have this method too that allows you to put in a whole URL string without having to get the querystring out of it</p>\n\n<pre><code>public static string SetParameterInUrl(string url, string paramName, object paramValue)\n{\n    int queryStringIndex = url.IndexOf("?");\n    string path;\n    string queryString;\n    if (queryStringIndex &gt;= 0 &amp;&amp; !url.EndsWith("?"))\n    {\n        path = url.Substring(0, queryStringIndex);\n        queryString = url.Substring(queryStringIndex + 1);\n    }\n    else\n    {\n        path = url;\n        queryString = string.Empty;\n    }\n    return path + "?" + SetParameter(queryString, paramName, paramValue);\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/47329', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3263/']