Mono 上的 Uri.MakeRelativeUri 行为
在 Mono (2.6.7) 上使用 MakeRelativeUri 时,我看到一些奇怪的行为(无论如何对我来说)。举个例子:
var uri1 = new Uri("/somepath/someothersubpath/");
var uri2 = new Uri("/somepath/img/someimg.jpg");
var uri3 = uri1.MakeRelativeUri(uri2);
Console.WriteLine(uri3.OriginalString);
我希望输出 "../img/someimg.jpg"
,但我得到 "img/someimg.jpg"
使用Windows/Visual Studio确认,如果在字符串的开头附加一个额外的斜杠,他会得到我预期的结果(我也尝试过这一点,但无济于事)。
我不确定这是否是 Mono 中 Uri 类的问题,或者我对 URI 类应该如何工作的理解是否有缺陷,但任何可以帮助我获得预期输出的建议将不胜感激。
谢谢,
亚历克斯
I'm seeing some strange (to me anyway) behavior when using MakeRelativeUri on Mono (2.6.7). Take the following example:
var uri1 = new Uri("/somepath/someothersubpath/");
var uri2 = new Uri("/somepath/img/someimg.jpg");
var uri3 = uri1.MakeRelativeUri(uri2);
Console.WriteLine(uri3.OriginalString);
I'd expect this to output "../img/someimg.jpg"
, but I get "img/someimg.jpg"
A friend has confirmed using windows/visual studio that he gets my expected result if appending an extra slash to the beginning of the string (I've tried this as well, and to no avail).
I'm not sure if this is an issue with the Uri class in mono, or if my understanding of how the URI class should work is flawed, but any advice that can help me get to the expected output would be greatly appreciated.
Thanks,
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来它与
someothersubpath
本身相关,而不是与其子路径相关。我不确定,但也许可以通过将任何内容附加到第一个字符串来解决这个问题:
It seems like it's making it relevant to
someothersubpath
itself, not to its children.I'm not sure about it, but maybe it's possible to get around this by appending anything to the first string:
Uri.MakeRelativeUri 的 Microsoft .NET 文档说它应该针对相对 Uris 抛出 InvalidOperationException。
正如所写的,您的代码在第一行抛出异常:“无效的 URI:无法确定 URI 的格式。”如果我修改代码:
那么最后一行会抛出
InvalidOperationException
: 相对 URI 不支持此操作。正如文档所说的那样。因此,Mono 实现似乎与 .NET 文档不一致。
The Microsoft .NET documentation for Uri.MakeRelativeUri says that it should throw
InvalidOperationException
for relative Uris.Your code, as written, throws an exception on the first line: "Invalid URI: The format of the URI could not be determined." If I modify the code:
Then the last line throws
InvalidOperationException
: This operation is not supported for a relative URI. Just as the documentation says it should.So it appears that the Mono implementation is at odds with the .NET documentation.