I recently had a requirement to place an ampersand in a querystring as a value, now this is handled perfectly well by UrlEncoding when using the Request.QueryString() method, however, when dealing with a Uri.Query, it goes a bit wrong. When inspecting the Uri.Query value even after UrlEncoding the ampersand, %26 ("&" when UrlEncoded) has been UrlDecoded already, before you have a chance to do anything about it.
The way round this that I found is to HtmlEncode the ampersand too, so it ends up as "%26amp%3b", e.g.
string theUrl = "http://www.domain.co.uk/pagename.aspx?qskey=" + HttpUtility.UrlEncode(HttpUtility.HtmlEncode("lorem & ipsum"));
Then to grab it out of the Uri.Query value do the following
HttpUtility.HtmlDecode(this.Query.Replace("&", "%26"))
Now the querystring & delimiters show as "&"s and the encoded one as "%26".
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5