Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want remove "Language" querystring from my url. How can I do this? (using Asp.net 3.5 , c#)

Default.aspx?Agent=10&Language=2 

I want to remove "Language=2", but language would be the first,middle or last. So I will have this

Default.aspx?Agent=20 
like image 355
Barbaros Alp Avatar asked Feb 09 '09 19:02

Barbaros Alp


People also ask

What is QueryString in C#?

A query string is a collection of characters input to a computer or web browser. A Query String is helpful when we want to transfer a value from one page to another.

How do you pass a value in QueryString?

To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.


2 Answers

If it's the HttpRequest.QueryString then you can copy the collection into a writable collection and have your way with it.

NameValueCollection filtered = new NameValueCollection(request.QueryString); filtered.Remove("Language"); 
like image 112
xcud Avatar answered Oct 09 '22 15:10

xcud


Here is a simple 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 Request.QueryString collection is read only.

like image 44
Paulius Zaliaduonis Avatar answered Oct 09 '22 15:10

Paulius Zaliaduonis