gpt4 book ai didi

asp.net - 在 ASP.NET 中,为什么有 UrlEncode() 和 UrlPathEncode()?

转载 作者:行者123 更新时间:2023-12-03 22:06:50 27 4
gpt4 key购买 nike

在最近的一个项目中,我有幸解决了一个错误,该错误涉及文件名中有空格时无法加载图像。我想“多么简单的问题,我会UrlEncode()它!”但是,不!只需使用 UrlEncode()没有解决问题。

新问题是 HttpUtilities.UrlEncode()方法将空格 ( ) 切换为加号 ( + ) 而不是 %20就像浏览器想要的那样。所以file+image+name.jpg会返回 not-found 而 file%20image%20name.jpg被正确找到。

谢天谢地,一位同事指出HttpUtilities.UrlPathEncode()对我来说使用 %20用于空格而不是 + .

为什么有两种处理 Url 编码的方法?为什么有两个命令的行为如此不同?

最佳答案

UrlEncode与 QueryString 一起使用很有用,因为浏览器倾向于使用 +使用 GET 提交表单时,此处代替空格方法。

UrlPathEncode简单地替换所有不能在 URL 中使用的字符,例如 < , > .

两个 MSDN 链接都包含此引用:

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 - 在 ASP.NET 中,为什么有 UrlEncode() 和 UrlPathEncode()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7997934/

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