Click here to Skip to main content
15,879,348 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

 
Questionthanks Pin
Member 426894718-Oct-16 0:41
Member 426894718-Oct-16 0:41 
QuestionThank you Pin
Member 1235752428-Feb-16 9:28
Member 1235752428-Feb-16 9:28 
GeneralMy vote of 1 Pin
Member 1087396227-Jun-14 7:58
Member 1087396227-Jun-14 7:58 
GeneralMy vote of 1 Pin
Member 1087396227-Jun-14 7:56
Member 1087396227-Jun-14 7:56 
QuestionQuerstring Removal Pin
Minu H17-Mar-13 23:46
Minu H17-Mar-13 23:46 
GeneralMy vote of 5 Pin
Lechu8117-Dec-12 6:11
Lechu8117-Dec-12 6:11 
QuestionMy vote of 5 Pin
Gökmen BULUT5-Jun-12 23:50
Gökmen BULUT5-Jun-12 23:50 
GeneralMy vote of 5 Pin
_Dhull 29-Apr-12 20:35
_Dhull 29-Apr-12 20:35 
GeneralRe: > Even though the unanimous vote is that the collection shou... Pin
Ankur\m/12-Apr-11 18:29
professionalAnkur\m/12-Apr-11 18:29 
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 
> OP was talking about a single request

Ah, that's what's happened. We're talking about two different things Smile | :)

I read the thread too quickly. Based on Venkatesh's reply, I misunderstood this topic to address the problem of how to detect a page refresh and not initiate a duplicate action.

So now your technique makes sense Smile | :)


_____
p.s. Even though the unanimous vote is that the collection should stay read-only, I think OP was pretty clever to dig in and come up with that approach. Albeit misguided, it shows initiative Wink | ;-)
GeneralRe: :D <i>I'm not seeing how you're handling state here. Each p... Pin
Ankur\m/11-Apr-11 21:48
professionalAnkur\m/11-Apr-11 21:48 
GeneralRe: Oh, I doubt myself daily ;-) I'm not seeing how you're han... Pin
Jeff Bowman11-Apr-11 21:31
professionalJeff Bowman11-Apr-11 21:31 
GeneralRe: Thanks for waiting. Let me tell this once again - Changing ... Pin
Ankur\m/11-Apr-11 21:06
professionalAnkur\m/11-Apr-11 21:06 
GeneralRe: That'll be great, thanks, when you have time. I'd like to se... Pin
Jeff Bowman11-Apr-11 20:05
professionalJeff Bowman11-Apr-11 20:05 
GeneralRe: No I do not, but if you need I can write it for you. It is j... Pin
Ankur\m/11-Apr-11 18:19
professionalAnkur\m/11-Apr-11 18:19 
GeneralRe: :thumbsup: Thank you. :) Pin
Toniyo Jackson8-Apr-11 0:29
Toniyo Jackson8-Apr-11 0:29 
GeneralRe: That's the spirit! :thumbsup: Cheers! Pin
Ankur\m/8-Apr-11 0:27
professionalAnkur\m/8-Apr-11 0:27 
GeneralRe: Anyway, my first Tip/Trick got failure. Will do best next ti... Pin
Toniyo Jackson8-Apr-11 0:16
Toniyo Jackson8-Apr-11 0:16 
GeneralRe: No problem Ankur. You are correct. One day i was trying to d... Pin
Toniyo Jackson8-Apr-11 0:10
Toniyo Jackson8-Apr-11 0:10 
GeneralRe: As I said, it is kept readonly for a reason, do not change t... Pin
Ankur\m/8-Apr-11 0:00
professionalAnkur\m/8-Apr-11 0:00 
GeneralReason for my vote of 5 very good.. I was looking for this s... Pin
RelicV4-Aug-11 23:04
RelicV4-Aug-11 23:04 
GeneralRe: Thanks :) Pin
Toniyo Jackson4-Aug-11 23:30
Toniyo Jackson4-Aug-11 23:30 
GeneralNo problem Ankur. You are correct. One day i was trying to d... Pin
Toniyo Jackson8-Apr-11 0:09
Toniyo Jackson8-Apr-11 0:09 
GeneralWhy would anyone do this? If something is made Readonly, it'... Pin
Ankur\m/7-Apr-11 23:49
professionalAnkur\m/7-Apr-11 23:49 
GeneralRe: Still, its not bad to know how to delete querystring yes? :) Pin
Toniyo Jackson7-Apr-11 23:53
Toniyo Jackson7-Apr-11 23:53 

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.