Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove items from the query string for redirection?

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

Request.QueryString.Remove("foo")

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?

like image 463
Danimal Avatar asked Sep 09 '08 14:09

Danimal


4 Answers

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:

    var nvc = new NameValueCollection();

    nvc.Add(HttpUtility.ParseQueryString(Request.Url.Query));

    nvc.Remove("foo");

    string url = Request.Url.AbsolutePath;

    for (int i = 0; i < nvc.Count; i++)
        url += string.Format("{0}{1}={2}", (i == 0 ? "?" : "&"), nvc.Keys[i], nvc[i]);

    Response.Redirect(url);

Update:

Turns out we can simplify the code like so:

    var nvc = HttpUtility.ParseQueryString(Request.Url.Query);

    nvc.Remove("foo");

    string url = Request.Url.AbsolutePath + "?" + nvc.ToString();

    Response.Redirect(url);
like image 67
6 revs Avatar answered Oct 05 '22 23:10

6 revs


You'd have to reconstruct the url and then redirect. Something like this:

string url = Request.RawUrl;

NameValueCollection params = Request.QueryString;
for (int i=0; i<params.Count; i++)
{
    if (params[i].GetKey(i).ToLower() == "foo")
    {
        url += string.Concat((i==0 ? "?" : "&"), params[i].GetKey(i), "=", params.Get(i));
    }
}
Response.Redirect(url);

Anyway, I didn't test that or anything, but it should work (or at least get you in thye right direction)

like image 45
Ryan Farley Avatar answered Oct 06 '22 01:10

Ryan Farley


Response.Redirect(String.Format("nextpage.aspx?{0}", Request.QueryString.ToString().Replace("foo", "mangledfoo")));

I quick hack, saves you little. But foo will not be present for the code awaiting it in nextpge.aspx :)

like image 43
hollystyles Avatar answered Oct 05 '22 23:10

hollystyles


Interesting question. I don't see any real viable alternative to manually copying the collection since CopyTo will only allow you to get the values (and not the keys).

I think HollyStyles' Hack 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..

If the target page is not reading it, why do you need to remove it from the QueryString?

It will just be ignored?

Failing that, I think you would just need to bite the bullet and create a util method to alter the collection for you.

UPDATE - Following Response from OP

Ahhhh! I see now, yes, I have had similar problems with SiteMap performing full comparison of the string.

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.

This way you would never have to worry about such issues again :)

like image 36
Rob Cooper Avatar answered Oct 06 '22 00:10

Rob Cooper