Sunday, 11 December 2016

C# - Update Querystring Values in a Url

We will look into a solution to update querystring values in a Url.

using System.Web;

// url has the complete url with querystring 
// ex: http://bing.com?abc=1xyz=20
var uriBuilder = new UriBuilder(url);

var query = HttpUtility.ParseQueryString(uriBuilder.Query);

// Existing Querystring parameter xyz value will be updated from 20 to 10
query["xyz"] = "10";

// As QS Parameter hello doesn't existed, it will be added.
query["hello"] = "world";

// Set updated querystring to the Url.
uriBuilder.Query = query.ToString();

// New Url: http://bing.com?abc=1xyz=10&hello=world
Response.Redirect(uriBuilder.ToString()); 

Remember to import namespace "System.Web"

No comments:

Post a Comment