How can i remove item from querystring in asp.net using c#?


Question

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
1
54
2/9/2009 8:18:09 PM

Accepted Answer

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.

10
5/23/2017 12:18:15 PM

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon