Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear or Remove query string in ASP.Net

Tags:

c#

asp.net

I have a QueryString named 'flagEdit' and I want to remove it after fetching it's value. But when i try to remove it using

Request.QueryString.Clear(); 

or

Request.QueryString.Remove("editFlag"); 

This error occurs -

System.NotSupportedException: Collection is read-only.

So, I want to know how to remove query string after fetches it's value

like image 600
Piyush Avatar asked Jan 22 '14 10:01

Piyush


People also ask

How can remove query string value in asp net?

The only way to 'clear' the query string is to do a redirect to the same URL but without the query string part.

How do I get rid of query strings?

Go to the Performance tab on the left side of your dashboard. Click Browser Cache. Uncheck the box next to Prevent caching of objects after settings change. Check the box next to Remove query strings from static resources.

How do I clear a Querystring in VB net?

You need to first make the collection editable and then remove the querystring value.


2 Answers

Removing (Deleting) Querystring in ASP.NET

Request.QueryString.Remove("editFlag") 

If you do the above, you will get an error

collection is read-only.

So, we need to write the below code before deleting the query string.

Try this way

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

You can also try this way

    var nvc = HttpUtility.ParseQueryString(Request.Url.Query);     nvc.Remove("editFlag");     string url = Request.Url.AbsolutePath + "?" + nvc.ToString();      Response.Redirect(url); 

Hope this helps

like image 197
Amarnath Balasubramanian Avatar answered Sep 18 '22 14:09

Amarnath Balasubramanian


If you are concerned about future page postbacks running the same code you intended to run when querystring has a value, simply add a if(!Page.IsPostBack) condition.

like image 32
SanthoshM Avatar answered Sep 20 '22 14:09

SanthoshM