I need a class to manipulate URLs in asp.net WebForms
eg
Url url = new Url("http://www.stackoverflow.com");
if(!url.Params.Contains("user"))
url.Params.Add("user", "1");
I have tried NameValueCollections but they seem to be readonly as do a number of objects in HttpContext
NameValueCollection nv = Request.QueryString;
nv.Remove("ForeignLanguage");
nv.Add("ForeignLanguage", l.ID.ToString());
The above generates a Collection is readonly exception
I would use the
UriBuilderclass, in conjunction with the very handyHttpUtility.ParseQueryStringmethod:Note the trick with passing an empty string to the
ParseQueryStringmethod – this will give you a (writable) instance ofSystem.Web.HttpValueCollection, a non-public class that derives fromNameValueCollectionand (among other things) emits its contents in querystring format when you call itsToStringmethod.You could also manipulate the current url: