views:

1582

answers:

7

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?

+2  A: 
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 :)

HollyStyles
I +1'd this, I don't know who downmodded, I thought it was a reasonable hack?! :S
Rob Cooper
A: 

Can you clone the collection and then redirect to the page with the cloned (and modified) collection?

I know it's not much better than iterating...

Michael Haren
+5  A: 

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)

Ryan Farley
"without iterating through the collection" ?
Espo
Yeah, my answer is really a "you can't, you will have to rebuild it" since there's no way to do what he is asking without it. You could avoid the iteration and copy to a new array and remove the item, but you'll still have to iterate through it to create the URL to redirect to.
Ryan Farley
+2  A: 

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 :)

Rob Cooper
I havent downmoded anyone in this thread yet. But a downmod is labeled "this was not helpful", and so far very few of the answers has been helpfull so I can understand why some people might downmod them
Espo
Thanks for the correction, I guess someone's gaming or having a bad day we're still in Beta afterall :) So sharing the love, +1 back.
HollyStyles
@Espo, I was really referring to Holly's post, seemed like a reasonable hack.. Looked like someone had just come in and -1'ed everyone..
Rob Cooper
A: 
Danimal
A: 

The search page appends "&terms=" to the query string for highlighting, so it messes it up.

Only other option is a regex replace. If you know for sure that &terms is in the middle of the collection somewhere leave the trailing & in the regex, if you know for sure it's on the end then drop the trailing & and change the replacement string "&" to String.Empty

Response.Redirect(String.Format("nextpage.aspx?{0}", Regex.Replace(Request.QueryString.ToString(), "&terms=.*&", "&"));
HollyStyles
A: 

You can avoid touching the original query string by working on a copy of it instead after which you redirect the page to the a url contaning your modified query string from which a properly formatted string is generated. Here is the code below:

    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);
Ziad