As a comment to an Azure question just now, @smarx noted
@smarx是对Azure问题的评论
I think it's generally better to do blob.Uri.AbsoluteUri than blob.Uri.ToString().
我认为做blob.Uri通常更好。比blob.Uri.ToString AbsoluteUri()。
Is there a reason for this? The documentation for Uri.AbsoluteUri
notes that it "Gets the absolute URI", Uri.ToString()
"Gets a canonical string representation for the specified instance."
这有什么原因吗?的文档Uri。AbsoluteUri注意它会“获得绝对URI”,URI。tostring ()获取指定实例的规范字符串表示形式。
4 个解决方案
#1
76
Given for example:
给出的例子:
UriBuilder builder = new UriBuilder("http://somehost/somepath");
builder.Query = "somekey=" + HttpUtility.UrlEncode("some+value");
Uri someUri = builder.Uri;
In this case, Uri.ToString()
will return a human-readable URL: http://somehost/somepath?somekey=some+value
在这种情况下,Uri.ToString()将返回一个人类可读的URL: http://somehost/somepath?
Uri.AbsoluteUri
on the other hand will return the encoded form as HttpUtility.UrlEncode returned it: http://somehost/somepath?somekey=some%2bvalue
Uri。另一方面,AbsoluteUri将返回编码形式为HttpUtility。UrlEncode返回:http://somehost/somepath?somekey=some%2bvalue
#2
22
Additionally: If your Uri
is a relative Uri
AbsoluteUri
will fail, ToString()
not.
另外:如果您的Uri是相对Uri,那么绝对Uri将会失败,ToString()不会。
Uri uri = new Uri("fuu/bar.xyz", UriKind.Relative);
string str1 = uri.ToString(); // "fuu/bar.xyz"
string str2 = uri.AbsoluteUri; // InvalidOperationException
#3
4
Since everybody seems to think that uri.AbsoluteUri
is better, but because it fails with relative paths, then probably the universal way would be:
因为大家似乎都这么认为,uri。绝对uri更好,但是因为它在相对路径上失败了,所以可能通用的方法是:
Uri uri = new Uri("fuu/bar.xyz", UriKind.Relative);
string notCorruptUri = Uri.EscapeUriString(uri.ToString());
#4
3
Why not check and use the correct one?
为什么不检查并使用正确的呢?
string GetUrl(Uri uri) => uri?.IsAbsoluteUri == true ? uri?.AbsoluteUri : uri?.ToString();
#1
76
Given for example:
给出的例子:
UriBuilder builder = new UriBuilder("http://somehost/somepath");
builder.Query = "somekey=" + HttpUtility.UrlEncode("some+value");
Uri someUri = builder.Uri;
In this case, Uri.ToString()
will return a human-readable URL: http://somehost/somepath?somekey=some+value
在这种情况下,Uri.ToString()将返回一个人类可读的URL: http://somehost/somepath?
Uri.AbsoluteUri
on the other hand will return the encoded form as HttpUtility.UrlEncode returned it: http://somehost/somepath?somekey=some%2bvalue
Uri。另一方面,AbsoluteUri将返回编码形式为HttpUtility。UrlEncode返回:http://somehost/somepath?somekey=some%2bvalue
#2
22
Additionally: If your Uri
is a relative Uri
AbsoluteUri
will fail, ToString()
not.
另外:如果您的Uri是相对Uri,那么绝对Uri将会失败,ToString()不会。
Uri uri = new Uri("fuu/bar.xyz", UriKind.Relative);
string str1 = uri.ToString(); // "fuu/bar.xyz"
string str2 = uri.AbsoluteUri; // InvalidOperationException
#3
4
Since everybody seems to think that uri.AbsoluteUri
is better, but because it fails with relative paths, then probably the universal way would be:
因为大家似乎都这么认为,uri。绝对uri更好,但是因为它在相对路径上失败了,所以可能通用的方法是:
Uri uri = new Uri("fuu/bar.xyz", UriKind.Relative);
string notCorruptUri = Uri.EscapeUriString(uri.ToString());
#4
3
Why not check and use the correct one?
为什么不检查并使用正确的呢?
string GetUrl(Uri uri) => uri?.IsAbsoluteUri == true ? uri?.AbsoluteUri : uri?.ToString();