Uri constructor (Uri, String) not working correctly?

Here is 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 be https://127.0.0.1:8443/svn/CXB1/Validation/branches/test , but this is https://127.0.0.1:8443/svn/CXB1/Validation/test . I cannot understand why the Uri (Uri, string) constructor eats the last part of the path.

+6
source share
3 answers

Add slash after branches

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

The behavior you see is correct, because replacing the last part is a good idea if you want to change the file name.

I would add a backslash at the end of the first part. Then itโ€™s clear that this is a directory, otherwise it can be interpreted 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); 

Get this result:

  https://127.0.0.1:8443/svn/CXB1/Validation/branches/test 
+3
source

This is the expected behavior.

If in the browser you were on a page with a full URI as https://127.0.0.1:8443/svn/CXB1/Validation/branches , and if on this page you clicked on a link that had only href from test , you would get into https://127.0.0.1:8443/svn/CXB1/Validation/test . Here's how the relative URI is composed with the base URI.

On the other hand, if the first URI ended with / , then it will work as you expected.

+2
source

Source: https://habr.com/ru/post/952259/


All Articles