views:

9177

answers:

10

Hi,

I want remove "Language" querystring from my url. How can i do this ? (using Asp.net 3.5 , c#)

Default.aspx?Agent=10&Language=2

I want to remove "Language=2", but language would be the first,middle or last. so i will have this

Default.aspx?Agent=20
A: 

You're probably going to want use a Regular Expression to find the parameter you want to remove from the querystring, then remove it and redirect the browser to the same file with your new querystring.

RKitson
A: 

Yes, there are no classes built into .NET to edit query strings. You'll have to either use Regex or some other method of altering the string itself.

David Morton
+1  A: 

You don't make it clear whether you're trying to modify the Querystring in place in the Request object. Since that property is read-only, I guess we'll assume you just want to mess with the string.

... In which case, it's borderline trivial.

  • grab the querystring off the Request
  • .split() it on '&'
  • put it back together into a new string, while sniffing for and tossing out anything starting with "language"
Jason Kester
Zhaph - Ben Duguid
I long ago built my own LinkUri class that can tear apart urls, build them off of key/value, and .tostring() them into URLs again. Basically, it does what you wish UriBuilder did. Took a morning to put together 4 years ago, and I use it every single day.
Jason Kester
I am on it know, i hate playing with strings, ohh god :) But with this way i feel most comfortable
Barbaros Alp
... so this example would be:return (new LinkUri(Request.Url)).Remove("language").ToString();
Jason Kester
+1  A: 

Get the querystring collection, parse it into a (name=value pair) string, excluding the one you want to REMOVE, and name it newQueryString

Then call Response.Redirect(known_path?newqueryString);

Pita.O
this was the first thing i have tried, thank you.
Barbaros Alp
+1  A: 

If it's the HttpRequest.QueryString then you can copy the collection into a writable collection and have your way with it.

NameValueCollection filtered = new NameValueCollection(request.QueryString);
filtered.Remove("Language");
xcud
A: 

Please, explain more of what your purpose are.

I do not think you are looking for a way to edit the users address field, are you?

Martin Bring
+2  A: 

I answered a similar question a while ago. Basically, the best way would be to use the class HttpValueCollection, which the QueryString property actually is, unfortunately it is internal in the .NET framework. You could use Reflector to grab it (and place it into your Utils class). This way you could manipulate the query string like a NameValueCollection, but with all the url encoding/decoding issues taken care for you.

HttpValueCollection extends NameValueCollection, and has a constructor that takes an encoded query string (ampersands and question marks included), and it overrides a ToString() method to later rebuild the query string from the underlying collection.

hmemcpy
I have read your post, and it sounds great. Thanks for your answer, i ll try it and get back
Barbaros Alp
+4  A: 

Finally,

hmemcpy answer was totally for me and thanks to other friends who answered.

I grab the HttpValueCollection using Reflector and wrote the following code

        var hebe = new HttpValueCollection();
        hebe.Add(HttpUtility.ParseQueryString(Request.Url.Query));

        if (!string.IsNullOrEmpty(hebe["Language"]))
            hebe.Remove("Language");

        Response.Redirect(Request.Url.AbsolutePath + "?" + hebe );
Barbaros Alp
+2  A: 

My personal preference here is rewriting the query or working with a namevaluecollection at a lower point, but there are times where the business logic makes neither of those very helpful and sometimes reflection really is what you need. In those circumstances you can just turn off the readonly flag for a moment like so:

// reflect to readonly property
PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);

// make collection editable
isreadonly.SetValue(this.Request.QueryString, false, null);

// remove
this.Request.QueryString.Remove("foo");

// modify
this.Request.QueryString.Set("bar", "123");

// make collection readonly again
isreadonly.SetValue(this.Request.QueryString, true, null);
annakata
A: 

Hi annakata!! your code works..but the problem is i need to postback it once again to work. if write the code in a button click then it does not remove the query string. if i click on the button once again ..it works . why is that an extra postback is needed for it to work.

Sathish