2

In my MVC applciation, sometimes I'm creating my querystring dynamically: once from javascript, and once from c# server code. In javascript, I'm using encodeURIComponent(value), and from the c#, I'm using HttpUtility.UrlEncode(value) method from System.Web

Suppose, I have space in the value, ex: a q:

  • encodeURIComponent(value) returns a%20q
  • HttpUtility.UrlEncode(value) returns a+q

Space is encoded differently, using these two methods. Is that any reason why is it working this way, different that "it was written like that long time ago and stays untouched" ?

Additionally, suppose I have plus in the value, ex: a+q

  • encodeURIComponent(value) returns a%2Bq
  • HttpUtility.UrlEncode(value) returns a%2bq

The js method returns upper case encoded characters, while c# one returns lower case ones.

What is the best way to cooperate in parallel with these two methods, to make the application behave in coherent way ? Shall I write alternative c# method to imitate the js method behaviour ? I suppose the decoding methods has the same differences. Is there any "correct" way work with these ?

jwaliszko
  • 16,942
  • 22
  • 92
  • 158
  • WIll you ever need to decode a URI with one of these that was encoded with the otehr (e.g. does the C# code need to decode a URI encoded in JS?) – Steve Apr 30 '12 at 09:24
  • Both are valid encodings according to the spec. Don't worry, be happy! – Richard Schneider Apr 30 '12 at 09:25
  • @Steve Haigh: I'm building parts of page in jquery dynamically, using parameters in querystring. Sometimes the querystring is created in razor view, while sometimes in javascript. Since the url looks differently, I need to cope with this at client side, so basically yes. – jwaliszko Apr 30 '12 at 09:31
  • @Richard Schneider: I have no tendency to be unhappy. Stay happy either! – jwaliszko Apr 30 '12 at 09:33

1 Answers1

3

Percent-encoding is defined in RFC3986. It defines that a space character is encoded as %20. However, query strings are most often in application/x-www-form-urlencoded format, which also allows encoding spaces as + instead of %20. If the decoder expects the query string in this format, then an encoder can choose whichever it prefers.

You can use the Uri.EscapeDataString Method if you want spaces always to be encoded as %20:

var result = Uri.EscapeDataString("a q");
// result == "a%20q"

On upper-case vs lower-case, RFC3986 says:

The uppercase hexadecimal digits 'A' through 'F' are equivalent to the lowercase digits 'a' through 'f', respectively. If two URIs differ only in the case of hexadecimal digits used in percent-encoded octets, they are equivalent.

For consistency, URI producers and normalizers should use uppercase hexadecimal digits for all percent-encodings.

var result = Uri.EscapeDataString("a+q");
// result == "a%2Bq"
Community
  • 1
  • 1
dtb
  • 213,145
  • 36
  • 401
  • 431
  • Thanks for comprehensive explanation. – jwaliszko Apr 30 '12 at 09:39
  • Based on your comment I've found related thread, which also explains similiar confusion: http://stackoverflow.com/questions/602642/server-urlencode-vs-httputility-urlencode – jwaliszko Apr 30 '12 at 09:46
  • It's a pity that Uri.EscapeDataString() does not encode "+" mark as encodeURIComponent() does. Instead of %2b it stays unchanged. Is there any encodeURIComponent() equivalent for c# ? – jwaliszko Apr 30 '12 at 09:55