Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Uri(Uri,字符串)构造函数不能正常工作?_C#_.net - Fatal编程技术网

C# Uri(Uri,字符串)构造函数不能正常工作?

C# Uri(Uri,字符串)构造函数不能正常工作?,c#,.net,C#,.net,以下是我代码的一部分: Uri branches = new Uri(@"https://127.0.0.1:8443/svn/CXB1/Validation/branches"); Uri testBranch = new Uri(branches, "test"); 我预计testbranchs将https://127.0.0.1:8443/svn/CXB1/Validation/branches/test,但它是https://127.0.0.1:8443/svn/CXB1/Valida

以下是我代码的一部分:

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

我预计
testbranchs
https://127.0.0.1:8443/svn/CXB1/Validation/branches/test
,但它是
https://127.0.0.1:8443/svn/CXB1/Validation/test
。我不明白为什么Uri(Uri,string)构造函数会吃掉路径的最后一部分。

在分支后添加斜杠

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

这是预期的行为

如果在浏览器中,您所在的页面的完整URI为
https://127.0.0.1:8443/svn/CXB1/Validation/branches
,如果您在该页面上单击了一个href为
test
的链接,您将被带到
https://127.0.0.1:8443/svn/CXB1/Validation/test
。这就是相对URI与基本URI的组合方式


另一方面,如果第一个URI以
/
结尾,那么它将按照您的预期工作。

您看到的行为是正确的,因为如果您想更改文件名,替换最后一部分是一个好主意

我会在第一部分末尾加上反斜杠。很明显,这是一个目录,否则它可能会被解释为一个文件

Uri branches = new Uri(@"https://127.0.0.1:8443/svn/CXB1/Validation/branches/");
Uri testBranch = new Uri(branches, "test");
Console.WriteLine(testBranch);
将获得以下输出:

 https://127.0.0.1:8443/svn/CXB1/Validation/branches/test

尝试
Uri分支=新Uri(@“https://127.0.0.1:8443/svn/CXB1/Validation/branches/");
Uri
不是路径,它与
路径的工作方式不同。请合并
。(请参阅注释)在第一个url末尾添加斜杠也没有效果,因为Uri(字符串)构造函数会将其删除。而
分支机构将
https://127.0.0.1:8443/svn/CXB1/Validation/branches
对不起,我在代码的其他部分发现了一个问题。添加斜杠很好。谢谢。是否有任何官方文件(即尾随斜杠)?我找不到任何。是否有任何官方文档(即URI应该以这种方式运行)?我找不到任何字符。@RovinBhandari-是的,第二个项目符号似乎已经覆盖了它-在添加新文本之前,您删除最后一个
/
右侧的所有字符。