带有空路径的 Uri [英] Uri with empty Path

查看:26
本文介绍了带有空路径的 Uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 System.Uri 来表示具有空路径组件的 URL?

Is is possible to represent a URL with an empty path component with System.Uri?

每当我创建

new Uri("http://example.com")

.Net (4) 似乎坚持在尾部添加斜杠,从而创建不同的 URL.换句话说,

.Net (4) seems to insist on appending a trailing slash, thereby creating a different URL. In other words,

new Uri("http://example.com").ToString()

返回http://example.com/"和

new Uri("http://example.com").Equals(new Uri("http://example.com/")) == true

可以抑制这种破坏行为(根据 RFC 3986)还是我必须推出自己的课程?

Can this broken behavior (according to RFC 3986) be suppressed or do I have to roll my own class?

更新:

看起来我在第 6.2.3 节中遗漏了这一点:

Looks like I missed this in section 6.2.3:

一般来说,URI 使用通用语法表示具有
的权限空路径应规范化为/"路径.

In general, a URI that uses the generic syntax for authority with an
empty path should be normalized to a path of "/".

措辞表明这是可选的.所以问题仍然存在:可以让 Uri 不这样做吗?

The wording indicates this is optional however. So the question remains: can Uri be made to NOT do this?

推荐答案

从 .NET 2.0 开始,您可以通过 OriginalString 属性.Uri 对象序列化时,原始字符串丢失.

Since .NET 2.0 you can get the original string passed to the constructor via OriginalString property. The original string is lost when Uri object is serialized.

如果这不是您想要的,没有帮助.EqualsToString 方法将始终使用规范化版本.

If this is not what you want, there is no help. Equals and ToString methods will always use the normalized version.

这篇关于带有空路径的 Uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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