Rectangle 27 0

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


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.

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.

I have read your post, and it sounds great. Thanks for your answer, i ll try it and get back

Not sure if this was the only method available in 2009 but there's another way without reflection in another answer for this question: stackoverflow.com/a/7530346/169034

Note
Rectangle 27 0

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


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.

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.

I have read your post, and it sounds great. Thanks for your answer, i ll try it and get back

Not sure if this was the only method available in 2009 but there's another way without reflection in another answer for this question: stackoverflow.com/a/7530346/169034

Note
Rectangle 27 0

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


public static string GetQueryStringWithOutParameter(string parameter)
    {
        var nameValueCollection = System.Web.HttpUtility.ParseQueryString(HttpContext.Current.Request.QueryString.ToString());
        nameValueCollection.Remove(parameter);
        string url = HttpContext.Current.Request.Path + "?" + nameValueCollection;

        return url;
    }
QueryString.ToString()
Request.QueryString

Here is a simple way. Reflector is not needed.

I was going nuts with QueryString.. Peace!!! Thanks man

One note here is that the line string url = HttpContext.Current.Request.Path + "?" + nameValueCollection; will encode the nameValueCollection, so sometimes spaces become "+", which might not be the right syntax for the request you are about to make. In my case, it wasn't. I had to map the keys and values in a for loop instead.

Note
Rectangle 27 0

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


public static string GetQueryStringWithOutParameter(string parameter)
    {
        var nameValueCollection = System.Web.HttpUtility.ParseQueryString(HttpContext.Current.Request.QueryString.ToString());
        nameValueCollection.Remove(parameter);
        string url = HttpContext.Current.Request.Path + "?" + nameValueCollection;

        return url;
    }
QueryString.ToString()
Request.QueryString

Here is a simple way. Reflector is not needed.

I was going nuts with QueryString.. Peace!!! Thanks man

One note here is that the line string url = HttpContext.Current.Request.Path + "?" + nameValueCollection; will encode the nameValueCollection, so sometimes spaces become "+", which might not be the right syntax for the request you are about to make. In my case, it wasn't. I had to map the keys and values in a for loop instead.

Note
Rectangle 27 0

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


HttpContext.Request.QueryString
NameValueCollection
System.Web.HttpUtility.ParseQueryString()
param1=1&param_to_remove=stuff&param2=2
var queryParams = System.Web.HttpUtility.ParseQueryString(HttpContext.Request.QueryString.ToString());
queryParams.Remove("param_to_remove");
string queryString = string.Join("&", queryParams.Cast<string>().Select(e => e + "=" + queryParams[e]));
  • Use case the query parameters back to a string and use string.Join() to format the query string as something readable by your URL as valid query parameters.
  • You can then use the Remove() function to remove the specific parameter (using the key to reference that parameter to remove).

I did however add a bit of explanation in case people didn't understand the process.

Now your query string should be param1=1&param2=2.

See below for a working example, where param_to_remove is the parameter you want to remove.

This is almost identical to PauliusZaliaduonis's answer. The third line of your code is a bit more complicated.

Note
Rectangle 27 0

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


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

@Skuli - You're modifying something which isn't intended for modification, and you're doing that by changing a state flag. There's some risk that you might do something destructive if you forget to "close" the reflection change, but it's pretty low and would require a developer to be pretty unaware of what the code is doing. Request here is scoped through IHttpHandler, so any old System.Web.Page can do this.

Great stuff. Is this not dangerous at all? What is the scope of this.Request.QueryString in this context?

I am using this in a case where I have a UserControl I can't modify that uses a query string parameter, and the only way to change the way that UserControl behaves is to change the query string before it processes it (I also can't change the actual incoming URL, I have to modify it during the the load cycle.) Very helpful, and it works great.

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:

Note
Rectangle 27 0

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


HttpContext.Request.QueryString
NameValueCollection
System.Web.HttpUtility.ParseQueryString()
param1=1&param_to_remove=stuff&param2=2
var queryParams = System.Web.HttpUtility.ParseQueryString(HttpContext.Request.QueryString.ToString());
queryParams.Remove("param_to_remove");
string queryString = string.Join("&", queryParams.Cast<string>().Select(e => e + "=" + queryParams[e]));
  • Use case the query parameters back to a string and use string.Join() to format the query string as something readable by your URL as valid query parameters.
  • You can then use the Remove() function to remove the specific parameter (using the key to reference that parameter to remove).

I did however add a bit of explanation in case people didn't understand the process.

Now your query string should be param1=1&param2=2.

See below for a working example, where param_to_remove is the parameter you want to remove.

This is almost identical to PauliusZaliaduonis's answer. The third line of your code is a bit more complicated.

Note
Rectangle 27 0

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


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

@Skuli - You're modifying something which isn't intended for modification, and you're doing that by changing a state flag. There's some risk that you might do something destructive if you forget to "close" the reflection change, but it's pretty low and would require a developer to be pretty unaware of what the code is doing. Request here is scoped through IHttpHandler, so any old System.Web.Page can do this.

Great stuff. Is this not dangerous at all? What is the scope of this.Request.QueryString in this context?

I am using this in a case where I have a UserControl I can't modify that uses a query string parameter, and the only way to change the way that UserControl behaves is to change the query string before it processes it (I also can't change the actual incoming URL, I have to modify it during the the load cycle.) Very helpful, and it works great.

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:

Note
Rectangle 27 0

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


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

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

Thanks for that Doug. I'm a bit baffled by the accepted answer. It sounds like questioner is causing another navigation to occur to get the unwanted parameter out of the querystring.

this is both simple and elegant - wish i could bump a couple of times

Note
Rectangle 27 0

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


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

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

Thanks for that Doug. I'm a bit baffled by the accepted answer. It sounds like questioner is causing another navigation to occur to get the unwanted parameter out of the querystring.

this is both simple and elegant - wish i could bump a couple of times

Note