Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have done a bit of research over the past day or so and I think I have some information on this.</p> <p>When you use Request.Querystring or HttpUtility.UrlDecode (or Encode) it is using the Encoding that is specified in the element (specifically the requestEncoding attribute) of the web.config (or the .config hierarchy if you haven't specified) ---NOT the Encoding.Default which is the default encoding for your server.</p> <p>When you have the encoding set to UTF-8, a single unicode character can be encoded as 2 %xx hex values. It will also be decoded that way when given the whole value.</p> <p>If you are UrlDecoding with a different Encoding than the url was encoded with, you will get a different result. </p> <p>Since HttpUtility.UrlEncode and UrlDecode can take an encoding parameter, its tempting to try to encode using an ANSI codepage, but UTF-8 is the right way to go if you have the browser support (apparently old versions don't support UTF-8). You just need to make sure that the is properly set and both sides will work fine.</p> <p>UTF-8 Seems to be the default encoding: (from .net reflector System.Web.HttpRequest)</p> <pre><code>internal Encoding QueryStringEncoding { get { Encoding contentEncoding = this.ContentEncoding; if (!contentEncoding.Equals(Encoding.Unicode)) { return contentEncoding; } return Encoding.UTF8; } } </code></pre> <p>Following the path to find out the this.ContentEncoding leads you to (also in HttpRequest)</p> <pre><code>public Encoding ContentEncoding { get { if (!this._flags[0x20] || (this._encoding == null)) { this._encoding = this.GetEncodingFromHeaders(); if (this._encoding == null) { GlobalizationSection globalization = RuntimeConfig.GetLKGConfig(this._context).Globalization; this._encoding = globalization.RequestEncoding; } this._flags.Set(0x20); } return this._encoding; } set { this._encoding = value; this._flags.Set(0x20); } } </code></pre> <p>To answer your specific question on the difference betwen Request.Url.Quer and Request.QueryString... here is how HttpRequest builds its Url Property:</p> <pre><code>public Uri Url { get { if ((this._url == null) &amp;&amp; (this._wr != null)) { string queryStringText = this.QueryStringText; if (!string.IsNullOrEmpty(queryStringText)) { queryStringText = "?" + HttpEncoder.CollapsePercentUFromStringInternal(queryStringText, this.QueryStringEncoding); } if (AppSettings.UseHostHeaderForRequestUrl) { string knownRequestHeader = this._wr.GetKnownRequestHeader(0x1c); try { if (!string.IsNullOrEmpty(knownRequestHeader)) { this._url = new Uri(this._wr.GetProtocol() + "://" + knownRequestHeader + this.Path + queryStringText); } } catch (UriFormatException) { } } if (this._url == null) { string serverName = this._wr.GetServerName(); if ((serverName.IndexOf(':') &gt;= 0) &amp;&amp; (serverName[0] != '[')) { serverName = "[" + serverName + "]"; } this._url = new Uri(this._wr.GetProtocol() + "://" + serverName + ":" + this._wr.GetLocalPortAsString() + this.Path + queryStringText); } } return this._url; } } </code></pre> <p>You can see it is using the HttpEncoder class to do the decoding, but it uses the same QueryStringEncoding value.</p> <p>Since I am already posting a lot of code here and anyone can get .NET Reflector, I'm going to snippet up the rest. The QueryString property comes from the HttpValueCollection which uses FillFromEncodedBytes method to eventually call HttpUtility.UrlDecode (with the QueryStringEncoding value set above), which eventually calls the HttpEncoder to decode it. They do seem to use different methodology to decode the actual bytes of the querystring, but the encoding they use to do it seems to be the same.</p> <p>It is interesting to me that the HttpEncoder has so many functions that seem to do the same thing, so its possible there are differences in those methods which can cause an issue.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload