6

Here is a part of my code:

Uri branches = new Uri(@"https://127.0.0.1:8443/svn/CXB1/Validation/branches");
Uri testBranch = new Uri(branches, "test");

I expect testBranches will be https://127.0.0.1:8443/svn/CXB1/Validation/branches/test, but it is https://127.0.0.1:8443/svn/CXB1/Validation/test. I can not understand why Uri(Uri, string) constructor eats the last part of the path.

Max
  • 170
  • 1
  • 10
  • 5
    try `Uri branches = new Uri(@"https://127.0.0.1:8443/svn/CXB1/Validation/branches/");` – I4V Aug 22 '13 at 07:51
  • 2
    `Uri` is not a path, it does not work the same way as `Path.Combine`. http://stackoverflow.com/a/1527643/284240 (see comments) – Tim Schmelter Aug 22 '13 at 07:56

3 Answers3

8

Add a slash after branches

  Uri branches = new Uri(@"https://127.0.0.1:8443/svn/CXB1/Validation/branches/");
  Uri testBranch = new Uri(branches, "test");
Kimtho6
  • 6,154
  • 9
  • 40
  • 56
  • Adding a slash at the end of the first url also have no effect because Uri(string) constructor removes it. And `branches` will be `https://127.0.0.1:8443/svn/CXB1/Validation/branches` – Max Aug 22 '13 at 08:04
  • Sorry, I've found a probem in other part of the code. Adding a slash works fine. Thank you. – Max Aug 22 '13 at 08:10
  • Is there any official documentation for this (i.e., the trailing slash)? I couldn't find any. – Rovin Bhandari Aug 09 '16 at 14:06
3

The Behaviour you see is correct, because replacing the last part is a good idea if you want to change the filename.

I would add the backslash at the end of the first part. Then it is clear that this is a directory, otherwise it may be interpretated as a file.

Uri branches = new Uri(@"https://127.0.0.1:8443/svn/CXB1/Validation/branches/");
Uri testBranch = new Uri(branches, "test");
Console.WriteLine(testBranch);

Will get this output:

 https://127.0.0.1:8443/svn/CXB1/Validation/branches/test
S.Spieker
  • 7,005
  • 8
  • 44
  • 50
2

That's the expected behaviour.

If, in a browser, you were on a page with its full URI as https://127.0.0.1:8443/svn/CXB1/Validation/branches, and if, on that page, you clicked on a link that just had an href of test, you would be taken to https://127.0.0.1:8443/svn/CXB1/Validation/test. This is how a relative URI is composed with a base URI.

On the other hand, if the first URI ended with a / then it would work as you seem to have expected.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448