<p>In my base page I need to remove an item from the query string and redirect. I can't use<br/></p>

<pre><code>Request.QueryString.Remove("foo")
</code></pre>

<p>because the collection is read-only. Is there any way to get the query string (except for that one item) without iterating through the collection and re-building it?</p>

[{'answer_id': 51981, 'author': 'hollystyles', 'author_id': 2083160, 'author_profile': 'https://Stackoverflow.com/users/2083160', 'pm_score': 2, 'selected': False, 'text': '<pre><code>Response.Redirect(String.Format("nextpage.aspx?{0}", Request.QueryString.ToString().Replace("foo", "mangledfoo")));\n</code></pre>\n\n<p>I quick hack, saves you little. But foo will not be present for the code awaiting it in nextpge.aspx :)</p>\n'}, {'answer_id': 51987, 'author': 'Michael Haren', 'author_id': 29, 'author_profile': 'https://Stackoverflow.com/users/29', 'pm_score': -1, 'selected': False, 'text': "<p>Can you clone the collection and then redirect to the page with the cloned (and modified) collection?</p>\n\n<p>I know it's not much better than iterating...</p>\n"}, {'answer_id': 51995, 'author': 'Ryan Farley', 'author_id': 1627, 'author_profile': 'https://Stackoverflow.com/users/1627', 'pm_score': 4, 'selected': True, 'text': '<p>You\'d have to reconstruct the url and then redirect. Something like this:</p>\n\n<pre><code>string url = Request.RawUrl;\n\nNameValueCollection params = Request.QueryString;\nfor (int i=0; i&lt;params.Count; i++)\n{\n    if (params[i].GetKey(i).ToLower() == "foo")\n    {\n        url += string.Concat((i==0 ? "?" : "&amp;"), params[i].GetKey(i), "=", params.Get(i));\n    }\n}\nResponse.Redirect(url);\n</code></pre>\n\n<p>Anyway, I didn\'t test that or anything, but it should work (or at least get you in thye right direction)</p>\n'}, {'answer_id': 52004, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 2, 'selected': False, 'text': '<p>Interesting question. I don\'t see any real viable alternative to manually copying the collection since <em>CopyTo</em> will only allow you to get the values (and not the keys).</p>\n<p>I think <a href="https://stackoverflow.com/questions/51964/how-do-i-remove-items-from-the-query-string-for-redirection#51981">HollyStyles\' Hack</a> would work (although I would be nervous about putting a Replace in a QueryString - obv. dependant on use case), but there is one thing thats bothering me..</p>\n<p><strong>If the target page is not reading it, why do you need to remove it from the QueryString?</strong></p>\n<p>It will just be ignored?</p>\n<p>Failing that, I think you would just need to bite the bullet and create a util method to alter the collection for you.</p>\n<h3>UPDATE - Following Response from OP</h3>\n<p>Ahhhh! I see now, yes, I have had similar problems with SiteMap performing full comparison of the string.</p>\n<p>Since changing the other source code (i.e. the search) is out of the question, I would probably say it may be best to do a Replace on the string. Although to be fair, if you often encounter code similar to this, it would equally be just as quick to set up a utility function to clone the collection, taking an array of values to filter from it.</p>\n<p>This way you would never have to worry about such issues again :)</p>\n'}, {'answer_id': 52128, 'author': 'hollystyles', 'author_id': 2083160, 'author_profile': 'https://Stackoverflow.com/users/2083160', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p><strong>The search page appends "&amp;terms=" to the query string for highlighting, so\n  it messes it up.</strong></p>\n</blockquote>\n\n<p>Only other option is a regex replace. If you <em>know for sure</em> that &amp;terms is in the middle of the collection somewhere leave the trailing &amp; in the regex, if you <em>know for sure</em> it\'s on the end then drop the trailing &amp; and change the replacement string "&amp;" to String.Empty</p>\n\n<pre><code>Response.Redirect(String.Format("nextpage.aspx?{0}", Regex.Replace(Request.QueryString.ToString(), "&amp;terms=.*&amp;", "&amp;"));\n</code></pre>\n'}, {'answer_id': 1632995, 'author': 'Ziad', 'author_id': 144227, 'author_profile': 'https://Stackoverflow.com/users/144227', 'pm_score': 3, 'selected': False, 'text': '<p>You can avoid touching the original query string by working on a copy of it instead. You can then redirect the page to the a url containing your modified query string like so:</p>\n\n<pre><code>    var nvc = new NameValueCollection();\n\n    nvc.Add(HttpUtility.ParseQueryString(Request.Url.Query));\n\n    nvc.Remove("foo");\n\n    string url = Request.Url.AbsolutePath;\n\n    for (int i = 0; i &lt; nvc.Count; i++)\n        url += string.Format("{0}{1}={2}", (i == 0 ? "?" : "&amp;"), nvc.Keys[i], nvc[i]);\n\n    Response.Redirect(url);\n</code></pre>\n\n<p><strong>Update:</strong></p>\n\n<p>Turns out we can simplify the code like so:</p>\n\n<pre><code>    var nvc = HttpUtility.ParseQueryString(Request.Url.Query);\n\n    nvc.Remove("foo");\n\n    string url = Request.Url.AbsolutePath + "?" + nvc.ToString();\n\n    Response.Redirect(url);\n</code></pre>\n'}, {'answer_id': 8169154, 'author': 'alex1kirch', 'author_id': 991442, 'author_profile': 'https://Stackoverflow.com/users/991442', 'pm_score': 2, 'selected': False, 'text': '<p><code>HttpUtility.ParseQueryString(Request.Url.Query)</code> return is<code>QueryStringValueCollection</code>. It is inherit from <code>NameValueCollection</code>.</p>\n\n<pre><code>    var qs = HttpUtility.ParseQueryString(Request.Url.Query);\n    qs.Remove("foo"); \n\n    string url = "~/Default.aspx"; \n    if (qs.Count &gt; 0)\n       url = url + "?" + qs.ToString();\n\n    Response.Redirect(url); \n</code></pre>\n'}, {'answer_id': 8919043, 'author': 'Answer', 'author_id': 1157369, 'author_profile': 'https://Stackoverflow.com/users/1157369', 'pm_score': 0, 'selected': False, 'text': '<p><code>Request.Url.GetLeftPart(UriPartial.Path)</code> should do this</p>\n'}, {'answer_id': 10412157, 'author': 'Chris', 'author_id': 94278, 'author_profile': 'https://Stackoverflow.com/users/94278', 'pm_score': 0, 'selected': False, 'text': '<p>I found this was a more elegant solution</p>\n\n<pre><code>var qs = HttpUtility.ParseQueryString(Request.QueryString.ToString());\nqs.Remove("item");\nConsole.WriteLine(qs.ToString());\n</code></pre>\n'}, {'answer_id': 16279198, 'author': 'biofractal', 'author_id': 776476, 'author_profile': 'https://Stackoverflow.com/users/776476', 'pm_score': 0, 'selected': False, 'text': '<p>Here is a solution that uses LINQ against the <code>Request.QueryString</code> which allows for complex filtering of qs params if required. </p>\n\n<p>The example below shows me filtering out the <code>uid</code> param to create a relative url ready for redirection.</p>\n\n<p>Before: <code>http://www.domain.com/home.aspx?color=red&amp;uid=1&amp;name=bob</code></p>\n\n<p>After: <code>~/home.aspx?color=red&amp;name=bob</code></p>\n\n<p><strong>Remove QS param from url</strong></p>\n\n<pre><code>var rq = this.Request.QueryString;\nvar qs = string.Join("&amp;", rq.Cast&lt;string&gt;().Where(k =&gt; k != "uid").Select(k =&gt; string.Format("{0}={1}", k, rq[k])).ToArray());\nvar url = string.Concat("~", Request.RawUrl.Split(\'?\')[0], "?", qs);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/51964', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2757/']