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
The only way to 'clear' the query string is to do a redirect to the same URL but without the query string part.
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.
You need to first make the collection editable and then remove the querystring value.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With