简体   繁体   中英

Uri with empty Path

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

Whenever I create

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

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

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

returns " http://example.com/ " and

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

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

Update:

Looks like I missed this in section 6.2.3:

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM