I know i can do this
var nv = HttpUtility.ParseQueryString(req.RawUrl);
But is there a way to convert this back to a url?
var newUrl = HttpUtility.Something("/page", nv);
I know i can do this
var nv = HttpUtility.ParseQueryString(req.RawUrl);
But is there a way to convert this back to a url?
var newUrl = HttpUtility.Something("/page", nv);
As @Atchitutchuk suggested, you can use QueryHelpers.AddQueryString in ASP.NET Core:
The short answer is to use
.ToString()
on theNameValueCollection
and combine it with the original url.However, I'd like to point out a few things:
You cant use
HttpUtility.ParseQueryString
onRequest.RawUrl
. TheParseQueryString()
method is looking for a value like this:?var=value&var2=value2
.If you want to get a
NameValueCollection
of theQueryString
parameters just useRequest.QueryString()
.To rebuild the URL just use nv.ToString().
If you are trying to parse a url string instead of using the
Request
object useUri
and theHttpUtility.ParseQueryString
method.Because a
NameValueCollection
can have multiple values for the same key, if you are concerned with the format of the querystring (since it will be returned as comma-separated values rather than "array notation") you may consider the following.Example
Turn into:
key1=val1&key2[]=val2&empty&key2[]=val2b
rather thankey1=val1&key2=val2,val2b&empty
.Code
or if you don't like Linq so much...
As has been pointed out in comments already, with the exception of this answer most of the other answers address the scenario (
Request.QueryString
is anHttpValueCollection
, "not" aNameValueCollection
) rather than the literal question.Update: addressed null value issue from comment.
Actually, you should encode the key too, not just value.
I always use UriBuilder to convert an url with a querystring back to a valid and properly encoded url.
Simply calling
ToString()
on theNameValueCollection
will return the name value pairs in aname1=value1&name2=value2
querystring ready format. Note thatNameValueCollection
types don't actually support this and it's misleading to suggest this, but the behavior works here due to the internal type that's actually returned, as explained below.Thanks to @mjwills for pointing out that the
HttpUtility.ParseQueryString
method actually returns an internalHttpValueCollection
object rather than a regularNameValueCollection
(despite the documentation specifyingNameValueCollection
). TheHttpValueCollection
automatically encodes the querystring when usingToString()
, so there's no need to write a routine that loops through the collection and uses theUrlEncode
method. The desired result is already returned.With the result in hand, you can then append it to the URL and redirect:
Currently the only way to use a
HttpValueCollection
is by using theParseQueryString
method shown above (other than reflection, of course). It looks like this won't change since the Connect issue requesting this class be made public has been closed with a status of "won't fix."As an aside, you can call the
Add
,Set
, andRemove
methods onnameValues
to modify any of the querystring items before appending it. If you're interested in that see my response to another question.