【发布时间】:2011-09-05 12:06:45
【问题描述】:
假设我想从以下字符串创建一个 Uri 对象:
string url = @"http://someserver.com?param1=1&url=http%3a%2f%2fwww.otherserver.com";
Uri uri = new Uri(url, UriKind.Absolute);
预期结果是:
http://someserver.com?param1=1&url=http%3a%2f%2fwww.otherserver.com
获得:
http://someserver.com/?param1=1&url=http://www.otherserver.com
在许多允许创建 Uri 的相关方法中发现了相同的行为:Uri.TryCreate、UriBuilder.Uri 等。
如何获得保留初始编码参数的 Uri?
【问题讨论】:
-
做什么的预期结果?如果我在控制台应用程序
Console.WriteLine(uri.AbsoluteUri);中尝试您的代码,则返回编码的 url -
其他相关搜索。它实际上是解码字符串的 ToString() 方法。 .OriginalString 或 .AbsoluteUri 将返回编码后的字符串。对于相对 url,使用 .PathAndQuery