Uri.AbsoluteUri vs. Uri.OriginalString

Viewed 6586

I recently became aware of the odd behavior of Uri.ToString() (namely, it unencodes some characters and therefore is primarily suitable for display purposes). I'm trying to decide between AbsoluteUri and OriginalString as my "go-to" for converting a Uri object to a string (e. g. in a razor view).

So far, the only difference I've found between the two is that AbsoluteUri will fail for relative uris (e. g. new Uri("foo", UriKind.Relative).AbsoluteUri). That seems like a point in favor of OriginalString. However, I'm concerned by the word "original", since it suggests that maybe some things will not get properly encoded or escaped.

Can anyone confirm the difference between these two properties (aside from the one difference I found)?

3 Answers

For converting a Uri object to a string I was using

Location.ToString().StripQuotes();

Note that ToString generated url string wrapped with double quotes ", and I had to remove them using StripQuotes from Flurl/src/Flurl/Util/CommonExtensions.cs

See also MSDN example that illustrates the difference between the value returned from OriginalString, which returns the string that was passed to the constructor, and from a call to ToString, which returns the canonical form of the string.

Related