在ASP.NET中,为什么会出现UrlEn code()和UrlPathEn code()? [英] In ASP.NET, why is there UrlEncode() AND UrlPathEncode()?
问题描述
在最近的一个项目,我不得不故障排除,涉及不加载时,空间是在图像文件名错误的乐趣。我想:多么简单的问题,我会 UrlEn code()
吧!但是,我的天!只需用 UrlEn code()
并没有解决问题。
In a recent project, I had the pleasure of troubleshooting a bug that involved images not loading when spaces were in the filename. I thought "What a simple issue, I'll UrlEncode()
it!" But, NAY! Simply using UrlEncode()
didn't resolve the problem.
新的问题是 HttpUtilities.UrlEn code()
方法交换空间(
),以加分( +
)的而不是的的%20
像浏览器通缉。因此,文件+图像+ name.jpg
将返回未找到,而文件%20image%20name.jpg
正确发现
The new problem was the HttpUtilities.UrlEncode()
method switched spaces () to plusses (
+
) instead of %20
like the browser wanted. So file+image+name.jpg
would return not-found while file%20image%20name.jpg
was found correctly.
值得庆幸的是,一个同事指出 HttpUtilities.UrlPathEn code()
来我它使用 20%
为空格代替 +
。
Thankfully, a coworker pointed out HttpUtilities.UrlPathEncode()
to me which uses %20
for spaces instead of +
.
为什么有处理URL编码两种方式?为什么那些表现得如此不同的两个命令?
WHY are there two ways of handling Url encoding? WHY are there two commands that behave so differently?
推荐答案
UrlEn code 一>是一个查询字符串使用有用的浏览器倾向于使用 +
在这里的地方提交的 GET $形式时,空间C $ C>方法。
UrlEncode is useful for use with a QueryString as browsers tend to use a +
here in place of a space when submitting forms with the GET
method.
<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httputility.urlpathen$c$c.aspx\">UrlPathEn$c$c简单地替换不能在URL中使用的所有字符,如&LT;
,&GT;
和
。
UrlPathEncode simply replaces all characters that cannot be used within a URL, such as <
, >
and .
这两个MSDN链接包括这句话:
Both MSDN links include this quote:
您可以连接code与UrlEn code方法或使用URL
UrlPathEn code方法。然而,这些方法返回不同的结果。
该UrlEn code方法每个空格字符转换为加号
(+)。该UrlPathEn code方法每个空格字符转换成
字符串%20,从而重新presents十六进制的空间。使用
在UrlPathEn code的方法,当你连接code在URL的路径部分
为了保证一致的德codeD网址,不管是哪个
平台或浏览器进行解码。
You can encode a URL using with the UrlEncode method or the UrlPathEncode method. However, the methods return different results. The UrlEncode method converts each space character to a plus character (+). The UrlPathEncode method converts each space character into the string "%20", which represents a space in hexadecimal notation. Use the UrlPathEncode method when you encode the path portion of a URL in order to guarantee a consistent decoded URL, regardless of which platform or browser performs the decoding.
这篇关于在ASP.NET中,为什么会出现UrlEn code()和UrlPathEn code()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!