Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Single URL query string value

Tags:

I have an ASP.NET page which takes a number of parameters in the query string:

search.aspx?q=123&source=WebSearch 

This would display the first page of search results. Now within the rendering of that page, I want to display a set of links that allow the user to jump to different pages within the search results. I can do this simply by append &page=1 or &page=2 etc.

Where it gets complicated is that I want to preserve the input query string from the original page for every parameter except the one that I'm trying to change. There may be other parameters in the url used by other components and the value I'm trying to replace may or may not already be defined:

search.aspx?q=123&source=WebSearch&page=1&Theme=Blue 

In this case to generate a link to the next page of results, I want to change page=1 to page=2 while leaving the rest of the query string unchanged.

Is there a builtin way to do this, or do I need to do all of the string parsing/recombining manually?

like image 971
star Avatar asked Sep 28 '10 14:09

star


People also ask

How do I replace a query string?

And if the original url is a string, we can just convert it to a uri and use it like this: public static string ReplaceQueryString(string url, string key, string value) { var uri = new UriBuilder(url); var t = HttpUtility. ParseQueryString(uri. Query); ...

How can I add or update a query string parameter?

set('PARAM_HERE', VALUE_HERE); history. pushState(null, '', url); This will preserve everything about the URL and only change or add the one query param. You can also use replaceState instead of pushState if you don't want it to create a new browser history entry.

How can change query string value in asp net?

So if you're asking how to deal with the value of a query string you just simply access it Request. QueryString["key"]. If you're wanting this 'change' in query string to be considered by the server you just need to effectively reload the page with the new value. So construct the url again page.


1 Answers

You can't modify the QueryString directly as it is readonly. You will need to get the values, modify them, then put them back together. Try this:

var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString()); nameValues.Set("page", "2"); string url = Request.Url.AbsolutePath; string updatedQueryString = "?" + nameValues.ToString(); Response.Redirect(url + updatedQueryString); 

The ParseQueryString method returns a NameValueCollection (actually it really returns a HttpValueCollection which encodes the results, as I mention in an answer to another question). You can then use the Set method to update a value. You can also use the Add method to add a new one, or Remove to remove a value. Finally, calling ToString() on the name NameValueCollection returns the name value pairs in a name1=value1&name2=value2 querystring ready format. Once you have that append it to the URL and redirect.

Alternately, you can add a new key, or modify an existing one, using the indexer:

nameValues["temp"] = "hello!"; // add "temp" if it didn't exist nameValues["temp"] = "hello, world!"; // overwrite "temp" nameValues.Remove("temp"); // can't remove via indexer 

You may need to add a using System.Collections.Specialized; to make use of the NameValueCollection class.

like image 105
Ahmad Mageed Avatar answered Oct 03 '22 15:10

Ahmad Mageed