作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我最近开始意识到 Uri.ToString()
的奇怪行为(即,它取消对某些字符进行编码,因此主要适用于显示目的)。我试图在 AbsoluteUri
和 OriginalString
之间做出决定,作为将 Uri
对象转换为字符串(例如,在 Razor View )。
到目前为止,我发现两者之间的唯一区别是 AbsoluteUri
对于相对 uris 会失败(例如 new Uri("foo", UriKind.Relative).AbsoluteUri
)。这似乎是支持 OriginalString
的观点。但是,我对“原始”这个词感到担忧,因为它暗示可能有些东西无法正确编码或转义。
谁能确认这两个属性之间的区别(除了我发现的一个区别)?
最佳答案
我总是喜欢 OriginalString
,因为我在 AbsoluteUri
方面遇到过多个问题。即:
var uri = new Uri("http://www.example.com/test%2F1");
Console.WriteLine(uri.OriginalString);
// http://www.example.com/test%2F1
Console.WriteLine(uri.AbsoluteUri);
// http://www.example.com/test/1 <-- WRONG
var uri = new Uri("http://www.example.com/test%2F1");
Console.WriteLine(uri.OriginalString);
// http://www.example.com/test%2F1
Console.WriteLine(uri.AbsoluteUri);
// http://www.example.com/test%2F1
var uri = new Uri("/test.aspx?v=hello world", UriKind.Relative);
Console.WriteLine(uri.OriginalString);
// /test.aspx?v=hello world
Console.WriteLine(uri.AbsoluteUri);
// InvalidOperationException: This operation is not supported for a relative URI.
var uri = new Uri("http://www.example.com/test.aspx?v=hello world");
Console.WriteLine(uri.OriginalString);
// http://www.example.com/test.aspx?v=hello world
Console.WriteLine(uri.AbsoluteUri);
// http://www.example.com/test.aspx?v=hello%20world <-- WRONG
关于c# - Uri.AbsoluteUri 与 Uri.OriginalString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35875376/
我最近开始意识到 Uri.ToString() 的奇怪行为(即,它取消对某些字符进行编码,因此主要适用于显示目的)。我试图在 AbsoluteUri 和 OriginalString 之间做出决定,作
我是一名优秀的程序员,十分优秀!