简体   繁体   English

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

[英]Uri constructor throws exception for relative path without UriKind

Why is 为什么是

 new Uri("temp/file.txt")

throwing the following exception 引发以下异常

UriFormatException: Invalid URI: The format of the URI could not be determined. UriFormatException:无效的URI:无法确定URI的格式。

instead of creating a relative Uri ? 而不是创建相对的Uri It doesn't know the format so why do I need to specify the UriKind.Relative argument? 它不知道格式,所以为什么需要指定UriKind.Relative参数? It still doesn't know the format when I do it. 当我这样做时,它仍然不知道格式。 This is counterintuitive. 这是违反直觉的。

Is there a way to create a relative Uri without having to use the UriKind argument? 有没有一种方法可以创建相对的Uri而不必使用UriKind参数? Having to do this untermines the purpose of the Uri class because I now cannot use it for implicit string --> Uri converstion without checking the string myself each time. 不必执行此操作就无法确定Uri类的用途,因为我现在无法将其用于隐式 string --> Uri对话,而无需自己检查string

您可能需要通过将RelativeOrAbsolute指定为UriKind来创建它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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