Eugene D. Gubenkov
Eugene D. Gubenkov

Reputation: 5357

.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?

Upvotes: 0

Views: 505

Answers (1)

Wagner DosAnjos
Wagner DosAnjos

Reputation: 6374

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;

Upvotes: 3

Related Questions