Uri 构造函数在没有 UriKind 的情况下为相对路径抛出异常

Uri constructor throws exception for relative path without UriKind

为什么是

 new Uri("temp/file.txt")

抛出以下异常

UriFormatException: Invalid URI: The format of the URI could not be determined.

而不是创建亲戚Uri?它不知道格式,所以为什么我需要指定 UriKind.Relative 参数?当我这样做时它仍然不知道格式。这是违反直觉的。

有没有办法在不使用 UriKind 参数的情况下创建相对 Uri?必须这样做会取消 Uri class 的目的,因为我现在不能在不检查 string 的情况下将它用于 implicit string --> Uri 转换] 每次都是我自己。

您可能希望通过将 RelativeOrAbsolute 指定为 UriKind:

来创建它
var uri = new Uri("temp/file.txt", UriKind.RelativeOrAbsolute);