使用 System.Uri 附加多个段 [英] Appending multiple segments with System.Uri

查看:29
本文介绍了使用 System.Uri 附加多个段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var baseUri = new Uri("http://localhost/");
var uri1 = new Uri(baseUri, "1");
var uri2 = new Uri(uri1, "2");   

出乎意料的是,uri2http://localhost/2.我将如何附加到 uri1 所以它是 http://localhost/1/2 ?Uri 是这样做的,还是我需要回退到字符串?顺便说一句,我尝试在几乎所有地方添加前导/尾随斜线.

Unexpectedly, uri2 is http://localhost/2. How would I append to uri1 so it's http://localhost/1/2 intead? Does Uri do this, or do I need to fallback to strings? Incidentally, I've tried adding leading/trailing slashes almost everywhere.

推荐答案

"1" 和 "2" 是 url 的文件名部分".如果你让1"看起来更像目录路径,它会正常工作1/":

"1" and "2" are "file name portion" of a url. If you make "1" to look more like directory path it will work ok "1/":

var baseUri = new Uri("http://localhost/");
var uri1 = new Uri(baseUri, "1/");
var uri2 = new Uri(uri1, "2"); 

注意:文件名部分"不是一个真正的术语,因为Url只有路径"和查询"组件,但通常路径的最后一块被视为文件名:/foo/bar/file.txt".文本".

Note: "file name portion" is not a real term, as Url only have "path" and "query" component, but normally last chunk of a path is treated as file name: "/foo/bar/file.txt".

当您组合 2 条路径时,您想用第二条路径替换第一条路径的某些尾部.在你的情况下,它最终只有文件名"段:/1"和2"(如果你把像/myFile.txt"和NewFile.txt"这样的真实值组合起来会更容易看看它为什么会这样).

When you combine 2 path you want to replace some tail portion of the first path with the second one. In your case it ends up to have just "file name" segment for both :"/1" and "2" (if you put real value like "/myFile.txt" and "NewFile.txt" in combining it would be easier to see why it behaves this way).

这篇关于使用 System.Uri 附加多个段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆