Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uri Constructor .NET Framework Bug?

Tags:

c#

.net

uri

Why the thirdRelativeUri fails? Is this a .NET bug? Seems like not fixed in 4.0 either.

var googleU = new Uri("http://www.google.com");
var secondRelativeUri = new Uri(googleU,"//test.htm"); // doesn't fail
var thirdRelativeUri = new Uri(googleU,"///test.htm"); // fails - Invalid URI: The hostname could not be parsed.

UPDATE:

@dariom pointed out that this is because protocol relative URL handling in .NET which make sense however this still seems buggy to me:

var thirdRelativeUri = new Uri("///test.htm",UriKind.Relative); // works as expected
var newUri = new Uri(googleU,thirdRelativeUri); //Fails, same error even though it's a relative URI

It fails even when the second Uri is Relative

like image 323
dr. evil Avatar asked Jul 18 '26 04:07

dr. evil


2 Answers

The file uri scheme (RFC 1738) file://[host]/path shows that host is optional. ///test.html would mean "Since this usually used for local files the host from RFC 1738 is often empty leading to a starting triple /. (ref)"

Change ///test.htm to file:///test.htm and the URI constructor will parse it properly. It's AbsolutePath will be /test.html.

Hope this helps.

like image 178
Brian Dishaw Avatar answered Jul 19 '26 18:07

Brian Dishaw


I think that the constructor is interpreting "//test.htm" as a URI with no scheme and a hostname of test.htm. You can see this by examining the value of secondRelativeUri - it's "http://test.htm/".

The third URI you're creating is invalid because you have too many slashes.

like image 39
dariom Avatar answered Jul 19 '26 16:07

dariom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!