65.9K
CodeProject is changing. Read more.
Home

Removing (Deleting) Querystring in ASP.NET

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.38/5 (15 votes)

Apr 7, 2011

CPOL
viewsIcon

132727

Removing (Deleting) Querystring in ASP.NET

We cannot delete a query string directly like below:

Request.QueryString.Remove("foo")

If you do this, you will get an error - collection is read-only. So, we need to write the below code before deleting the query string.

In C#:

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

In VB.NET:

Dim isreadonly As PropertyInfo = _
  GetType(System.Collections.Specialized.NameValueCollection).GetProperty(_
  "IsReadOnly", BindingFlags.Instance Or BindingFlags.NonPublic)

' make collection editable
isreadonly.SetValue(Me.Request.QueryString, False, Nothing)

' remove
Me.Request.QueryString.Remove("foo")
I hope that this will help you guys.