0

I've noticed that if you call Uri.ToString() when URI behind is URL with percent encoded spaces (%20), then you will have malformed URL as the result:

// result: "http://example.com/test segment"
new Uri("http://example.com/test%20segment").ToString();

Since URL is URI (opposite statement is not correct) and URL with non encoded spaces is not valid it looks like it's not safe to use Uri as the container for URL.

Is it correct behaviour?

Community
  • 1
  • 1
Eugene D. Gubenkov
  • 5,127
  • 6
  • 39
  • 71
  • Presumably `ToString()` is parsing the `%20` following the parent encoding spec, and determines that it should be a space. – James Gould Nov 23 '16 at 16:21
  • 2
    [MSDN](https://msdn.microsoft.com/en-us/library/system.uri.tostring(v=vs.110).aspx) says it should return the string with regular spaces. Use .OriginalString if you want the `%20` instead of spaces. – Tvde1 Nov 23 '16 at 16:21
  • 1
    The documentation you pointed out says `.ToString()` returns spaces while `.OriginalString` returns `%20`. – Wagner DosAnjos Nov 23 '16 at 16:24

1 Answers1

3

As per MSDN:

// result: "http://example.com/test segment"
new Uri("http://example.com/test%20segment").ToString();

// result: "http://example.com/test%20segment"
new Uri("http://example.com/test%20segment").OriginalString;
Wagner DosAnjos
  • 6,304
  • 1
  • 15
  • 29