I came across a problem in my current application that required fiddling with the query string in a base Page class (which all my pages inherit from) to solve the problem. Since some of my pages use the query string I was wondering if there is any class that provides clean and simple query string manipulation.
Example of code:
// What happens if I want to future manipulate the query string elsewhere // (e.g. maybe rewrite when the request comes back in) // Or maybe the URL already has a query string (and the ? is invalid) Response.Redirect(Request.Path + '?ProductID=' + productId);
Use
HttpUtility.ParseQueryString, as someone suggested (and then deleted).This will work, because the return value from that method is actually an
HttpValueCollection, which inheritsNameValueCollection(and is internal, you can’t reference it directly). You can then set the names/values in the collection normally (including add/remove), and callToString— which will produce the finished querystring, becauseHttpValueCollectionoverridesToStringto reproduce an actual query string.