我有一个"基本网址" (这是相对的)和一个"子URL"对于"基本URL"也是相对的。我想要正确地结合这些。例如:
"some/path/here" + "../other/path" => "some/other/path"
我如何在.NET中执行此操作? Uri
类要求第一个Uri是绝对的。
已添加:更多示例,以澄清。
"contents/styles/style.css" + "image.jpg" => "contents/styles/image.jpg"
"contents/folder/style.css" + "../image.jpg" => "contents/image.jpg"
"contents/folder/style.css" + "../images/image.jpg" => "contents/images/image.jpg"
换句话说,在原始示例中,由于here
没有尾部斜杠,因此应将其视为文件。因此,..
移动到文件夹some
。
答案 0 :(得分:3)
尝试:
UriBuilder builder = new UriBuilder();
Uri baseuri = builder.Uri;
builder.Path = "some/path/here";
Uri finalUri = baseuri.MakeRelativeUri(new Uri(builder.Uri, "..\\other\\path"));
或
Uri finalUri = baseuri.MakeRelativeUri(new Uri(builder.Uri, "file"));