views:

3636

answers:

6

Using the following code I get a nice formatted string:

Request.QueryString.ToString

Gives me something like: &hello=world&microsoft=sucks

But when I use this code to clone the collection to another object (of the same type) I get the Type() back from the ToString() method instead.

System.Collections.Specialized.NameValueCollection variables = new System.Collections.Specialized.NameValueCollection(Request.QueryString);
if (!string.IsNullOrEmpty(variables["sid"]))
    variables.Remove("sid");
Response.Write(variables.ToString());

Is there a tidier way to output it rather than looking and building the string manually?

A: 

Why do you want to copy the QueryString collection into a new NameValueCollection?

    if (!string.IsNullOrEmpty(Request.QueryString["sid"]))
        Request.QueryString.Remove("sid");

Yes indeed, i am wrong, it is read only. So the essence is to use the Remove Method on your NameValuecollection:

System.Collections.Specialized.NameValueCollection variables = new System.Collections.Specialized.NameValueCollection(Request.QueryString);
if (!string.IsNullOrEmpty(variables["sid"]))
    variables.Remove("sid");
Johannes Hädrich
Because I cannot remove from the Querystring collection as it is readonly
tigermain
corrected my answer ;-)
Johannes Hädrich
A: 

Request.QueryString actually return a HttpValueCollection object (which unfortuately, is internal to System.Web so you can't you it).

Nevertheless, HttpValueCollection is derived from NameValueCollection, and it's Remove() method remains intact, so you should be able to call Request.QueryString.Remove("sid");

James Curran
Unfortunately you can't do that as it is a readonly collection
tigermain
+2  A: 

Because it is actually a special NVC that is of type HTTPValueCollection. So when you call .ToString on it, it knows how to format it correctly.

Brian Schmitt
+1  A: 

You can also use Reflector to extract the HttpValueCollection class into your own, and use it then.

hmemcpy
I'm giving you the win here hmemcpy as its probably the most appropriate but it doesnt solve my problem, guess I'll just have to do it the dirty way
tigermain
Actually, you pretty much just need the HttpValueCollection.ToString(bool urlencoded, IDictionary excludeKeys) method.
James Curran
Yup, just like James said, the HttpValueCollection overrides ToString() to create the query string with HttpEncoded values and ampersands. You can use Add/Remove on the values, and later call ToString() to create your new query string.
hmemcpy
Can you tell me about Reflector ? is it the software which you can see the assemblies or something different?
Barbaros Alp
Yes Reflector lets you open .net Dll's/Exes and will do test to reproduce the code in it, there are plugins which will allow you to do other things like disassemble to a project
tigermain
+1  A: 

If you don't absolutely need a NameValueCollection, A dictionary offers a lot of the same semantics:

var variables = Request.QueryString.OfType<DictionaryEntry>()
    .Where(entry => entry.Key != "sid")
    .ToDictionary(entry => entry.Key, entry => entry.Value);
Jimmy
I think this won't work on query strings with duplicate keys.
Mauricio Scheffer
+10  A: 

HttpValueCollection is internal, but you can use "var" to declare it without extract it with reflector.

var query = HttpUtility.ParseQueryString(Request.Url.Query);
query["Lang"] = myLanguage; // Add or replace param
string myNewUrl = Request.Url.AbsolutePath + "?" + query;
Michele Bersini