How can I remove an item from querystring in asp.net using c #?

I want to remove the "Language" request from my URL. How can i do this? (using Asp.net 3.5, C #)

Default.aspx?Agent=10&Language=2 

I want to delete "Language = 2", but the language will be first, middle or last. so i will have it

 Default.aspx?Agent=20 
+52
c # query-string
09 Feb '09 at 19:35
source share
15 answers

I answered a similar question a while ago. In principle, the best way would be to use the HttpValueCollection class, which is actually a property of QueryString , unfortunately, it is internal to the .NET platform. You can use a Reflector to grab it (and put it in your Utils class). This way you can manipulate the query string, for example, NameValueCollection, but with all the problems of encoding / decoding the URLs that concern you.

HttpValueCollection extends NameValueCollection and has a constructor that takes an encoded query string (ampersands and question marks are included) and overrides the ToString() method to later rebuild the query string from the base collection.

+10
Feb 09 '09 at 19:59
source share

If it's HttpRequest.QueryString, then you can copy the collection to a writable collection and have your own path with it.

 NameValueCollection filtered = new NameValueCollection(request.QueryString); filtered.Remove("Language"); 
+114
Feb 09 '09 at 19:51
source share

Here is an easy way. Reflector is not needed.

  public static string GetQueryStringWithOutParameter(string parameter) { var nameValueCollection = System.Web.HttpUtility.ParseQueryString(HttpContext.Current.Request.QueryString.ToString()); nameValueCollection.Remove(parameter); string url = HttpContext.Current.Request.Path + "?" + nameValueCollection; return url; } 

Here QueryString.ToString() is required because the Request.QueryString collection is read-only.

+44
Sep 23 2018-11-11T00:
source share

Finally,

The hmemcpy answer was completely for me and thanks to the other friends who answered.

I grabbed an HttpValueCollection using Reflector and wrote the following code

  var hebe = new HttpValueCollection(); hebe.Add(HttpUtility.ParseQueryString(Request.Url.Query)); if (!string.IsNullOrEmpty(hebe["Language"])) hebe.Remove("Language"); Response.Redirect(Request.Url.AbsolutePath + "?" + hebe ); 
+36
09 Feb '09 at 21:35
source share

My personal preference here is to rewrite the query or work with the valuecollection name at the bottom, but there are times when the business logic does not do those that are very useful, and sometimes really reflect what you need. In these circumstances, you can simply turn off the readonly flag for a moment as follows:

 // reflect to readonly property 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"); // modify this.Request.QueryString.Set("bar", "123"); // make collection readonly again isreadonly.SetValue(this.Request.QueryString, true, null); 
+26
08 Oct '09 at 8:45
source share

Try it...

 PropertyInfo isreadonly =typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic); isreadonly.SetValue(this.Request.QueryString, false, null); this.Request.QueryString.Remove("foo"); 
+3
Jun 11 '13 at
source share

You will not indicate whether you are trying to change the Querystring in place of the Request object. Since this property is read-only, I assume that we assume that you just want to refer to a string.

... In this case, it borders on the trivial.

  • take the request from the request
  • .split () it on '&'
  • put it back together on a new line, sniffing and discarding anything starting with the "language"
+1
Feb 09 '09 at 19:44
source share

Get a collection of queries, parse it in a string ( name=value pair ), excluding the one you want to delete, and name it newQueryString

Then call Response.Redirect(known_path?newqueryString) ;

+1
Feb 09 '09 at 19:45
source share
  • Collect the query string using HttpContext.Request.QueryString . The default type is NameValueCollection .
  • Pass it as a string and use System.Web.HttpUtility.ParseQueryString() to parse the query string (which returns NameValueCollection again).
  • Then you can use the Remove() function to remove a specific parameter (using the key to refer to this parameter for removal).
  • Use the query parameters back to the string and use string.Join() to format the query string as something read by your URL as valid query parameters.

The following is a working example where param_to_remove is the parameter you want to remove.

Let's say your query parameters are param1=1¶m_to_remove=stuff¶m2=2 . Run the following lines:

 var queryParams = System.Web.HttpUtility.ParseQueryString(HttpContext.Request.QueryString.ToString()); queryParams.Remove("param_to_remove"); string queryString = string.Join("&", queryParams.Cast<string>().Select(e => e + "=" + queryParams[e])); 

Your query string should now be param1=1¶m2=2 .

+1
Feb 29 '16 at 23:33
source share

You will probably want to use a regular expression to find the parameter you want to remove from the query string, then delete it and redirect the browser to the same file with a new query.

0
Feb 09 '09 at 19:42
source share

Yes, in .NET there are no classes for editing query strings. You will have to either use Regex, or some other way to modify the string itself.

0
Feb 09 '09 at 19:42
source share

If you already have a query string as a string, you can also use simple string manipulations:

 int pos = queryString.ToLower().IndexOf("parameter="); if (pos >= 0) { int pos_end = queryString.IndexOf("&", pos); if (pos_end >= 0) // there are additional parameters after this one queryString = queryString.Substring(0, pos) + queryString.Substring(pos_end + 1); else if (pos == 0) // this one is the only parameter queryString = ""; else // this one is the last parameter queryString=queryString.Substring(0, pos - 1); } 
0
Nov 27 '12 at 3:20
source share
 string queryString = "Default.aspx?Agent=10&Language=2"; //Request.QueryString.ToString(); string parameterToRemove="Language"; //parameter which we want to remove string regex=string.Format("(&{0}=[^&\s]+|{0}=[^&\s]+&?)",parameterToRemove); string finalQS = Regex.Replace(queryString, regex, ""); 

https://regexr.com/3i9vj

0
Dec 20 '17 at 13:05
source share

Parse the Querystring into the NameValueCollection. Delete item. And use toString to convert it back to a query string.

 using System.Collections.Specialized; NameValueCollection filteredQueryString = System.Web.HttpUtility.ParseQueryString(Request.QueryString.ToString()); filteredQueryString.Remove("appKey"); var queryString = '?'+ filteredQueryString.ToString(); 
0
Apr 30 '19 at 4:29
source share

Well, I have a simple solution, but there is a little javascript.

assuming the query string is "ok = 1"

  string url = Request.Url.AbsoluteUri.Replace("&ok=1", ""); url = Request.Url.AbsoluteUri.Replace("?ok=1", ""); Response.Write("<script>window.location = '"+url+"';</script>"); 
-one
Feb 03 '15 at 9:06
source share



All Articles