加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Uri构造函数.NET Framework错误?

发布时间:2020-12-16 01:49:04 所属栏目:百科 来源:网络整理
导读:为什么thirdRelativeUri失败了?这是一个.NET错误吗?似乎也没有在4.0中修复. var googleU = new Uri("http://www.google.com");var secondRelativeUri = new Uri(googleU,"//test.htm"); // doesn't failvar thirdRelativeUri = new Uri(googleU,"///test.h
为什么thirdRelativeUri失败了?这是一个.NET错误吗?似乎也没有在4.0中修复.

var googleU = new Uri("http://www.google.com");
var secondRelativeUri = new Uri(googleU,"//test.htm"); // doesn't fail
var thirdRelativeUri = new Uri(googleU,"///test.htm"); // fails - Invalid URI: The hostname could not be parsed.

更新:

@dariom指出这是因为.NET中的协议相对URL处理有意义但是这对我来说仍然是错误的:

var thirdRelativeUri = new Uri("///test.htm",UriKind.Relative); // works as expected
var newUri = new Uri(googleU,thirdRelativeUri); //Fails,same error even though it's a relative URI

即使第二个Uri是相对的,它也会失败

解决方法

文件uri方案(RFC 1738)file:// [host] / path显示主机是可选的. ///test.html意味着“由于这通常用于本地文件,因此RFC 1738中的主机通常为空,导致启动三元组/ . (ref)”

将///test.htm更改为file:///test.htm,URI构造函数将正确解析它.它的AbsolutePath将是/test.html.

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读