Hugo
Hugo

Reputation: 535

Uri constructor

Edit: I rephrase the question to make it clearer

I'm trying to understand what's the behavior of the Uri(Uri, uri) constructor:

 new Uri(new Uri("http://mydomain.com/some/path"), new Uri("/another/path"))

I get the following result:

 "http://mydomain.com/another/path"

But I cannot find any doc explaining clearly what the creation rules are. In other words, is it guaranteed that i'll never get the following ?

 "http://mydomain.com/some/path/another/path"

unless I use as second parameter:

new Uri("another/path")

Upvotes: 0

Views: 1319

Answers (3)

Wouter de Kort
Wouter de Kort

Reputation: 39898

Your first Uri has to end with a '/'. The second one should have the leading '/' removed and be set to UriKind.Relative.

Uri a = new Uri("http://mydomain.com/some/path/");
Uri b = new Uri("another/path", UriKind.Relative);

Uri c = new Uri(a, b);

Console.WriteLine(c);

This will output:

http://mydomain.com/some/path/another/path

Upvotes: 1

quetzalcoatl
quetzalcoatl

Reputation: 33506

The problem is that first argument is the BASE and the second argument is the BASE-RELATIVE url.

Please look at the second one: it begins with a '/'. This means that the second one is ABSOLUTE, thus, only domainname is taken from the base, and the rest is taken literally.

If you want to make relative, then make it relative:

new Uri(new Uri("http://mydomain.com/some/path"), new Uri("another/path"))

please note that the leading / is missing now!

Upvotes: 0

Oded
Oded

Reputation: 498982

Your second Uri "/another/path" is rooted (it starts with /), so the Uri created will have the domain followed by it.

If your second Uri were "another/path", the result would be "http://mydomain.com/some/path/another/path".

Upvotes: 5

Related Questions