gpt4 book ai didi

c# - Uri.AbsoluteUri 与 Uri.OriginalString

转载 作者:太空狗 更新时间:2023-10-29 17:44:27 25 4
gpt4 key购买 nike

我最近开始意识到 Uri.ToString() 的奇怪行为(即,它取消对某些字符进行编码,因此主要适用于显示目的)。我试图在 AbsoluteUriOriginalString 之间做出决定,作为将 Uri 对象转换为字符串(例如,在 Razor View )。

到目前为止,我发现两者之间的唯一区别是 AbsoluteUri 对于相对 uris 会失败(例如 new Uri("foo", UriKind.Relative).AbsoluteUri)。这似乎是支持 OriginalString 的观点。但是,我对“原始”这个词感到担忧,因为它暗示可能有些东西无法正确编码或转义。

谁能确认这两个属性之间的区别(除了我发现的一个区别)?

最佳答案

我总是喜欢 OriginalString,因为我在 AbsoluteUri 方面遇到过多个问题。即:

AbsoluteUri 在 .NET 4.0 和 .NET 4.5 中的行为不同 ( see )

.NET Framework 4.0

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

.NET Framework 4.5

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

AbsoluteUri 不支持相对 URI

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.

AbsoluteUri 进行不需要的转义

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com