Click here to Skip to main content
15,881,139 members
Articles / Web Development / ASP.NET
Tip/Trick

Removing (Deleting) Querystring in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.38/5 (17 votes)
7 Apr 2011CPOL 127.2K   10   28
Removing (Deleting) Querystring in ASP.NET

We cannot delete a query string directly like below:


VB
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#:


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:


VB
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: > OP was talking about a single request Ah, that's what's h... Pin
Jeff Bowman12-Apr-11 7:46
professionalJeff Bowman12-Apr-11 7:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.