Eugene D. Gubenkov Eugene D. Gubenkov - 11 days ago 4
C# Question

.NET Uri.ToString() ruins the URL?

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?

Answer

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;
Comments