Uri.AbsoluteUri vs Uri.OriginalString

I recently found out about the odd behavior of Uri.ToString() (namely, it decrypts some characters and is therefore primarily suitable for displaying). I am trying to resolve between AbsoluteUri and OriginalString as a "go-to" to convert a Uri object to a string (for example, as a razor).

So far, the only difference I have found between the two is that AbsoluteUri will not work for relative uris (for example, new Uri("foo", UriKind.Relative).AbsoluteUri ). This seems like a point in favor of OriginalString . However, I am worried about the word β€œoriginal,” because it suggests that some things might not be encoded or shielded correctly.

Can anyone confirm the difference between these two properties (except for the one difference I found)?

+6
source share
2 answers

Normalization is a good reason to use AbsoluteUri over OriginalString :

 new Uri("http://foo.bar/var/../gar").AbsoluteUri // http://foo.bar/gar new Uri("http://foo.bar/var/../gar").OriginalString // http://foo.bar/var/../gar 
+5
source

I always use OriginalString since I ran into several problems with AbsoluteUri . Namely:

AbsoluteUri behaves differently in .NET 4.0 vs .NET 4.5 ( see )

.NET Framework 4.0

 var uri = new Uri("http://www.example.com/test%2F1"); Console.WriteLine(uri.OriginalString); // http://www.example.com/test%2F1 Console.WriteLine(uri.AbsoluteUri); // http://www.example.com/test/1 <-- WRONG 

.NET Framework 4.5

 var uri = new Uri("http://www.example.com/test%2F1"); Console.WriteLine(uri.OriginalString); // http://www.example.com/test%2F1 Console.WriteLine(uri.AbsoluteUri); // http://www.example.com/test%2F1 

AbsoluteUri does not support relative URIs

 var uri = new Uri("/test.aspx?v=hello world", UriKind.Relative); Console.WriteLine(uri.OriginalString); // /test.aspx?v=hello world Console.WriteLine(uri.AbsoluteUri); // InvalidOperationException: This operation is not supported for a relative URI. 

AbsoluteUri does unwanted shielding

 var uri = new Uri("http://www.example.com/test.aspx?v=hello world"); Console.WriteLine(uri.OriginalString); // http://www.example.com/test.aspx?v=hello world Console.WriteLine(uri.AbsoluteUri); // http://www.example.com/test.aspx?v=hello%20world <-- WRONG 
+2
source

All Articles