加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 00:02:20 所属栏目:asp.Net 来源:网络整理
导读:在最近的一个项目中,我有幸排除了一个错误,该错误涉及当文件名中有空格时图片无法加载.我想“这是一个简单的问题,我会UrlEncode()吧
在最近的一个项目中,我有幸排除了一个错误,该错误涉及当文件名中有空格时图片无法加载.我想“这是一个简单的问题,我会UrlEncode()吧!”但是,NAY!仅使用UrlEncode()无法解决问题.

新问题是HttpUtilities.UrlEncode()方法将空格()转换为plusses()而不是像浏览器想要的那样.因此,文件图像name.jpg被正确找到时,文件图像name.jpg将返回not-found.

值得庆幸的是,一位同事向我指出了HttpUtilities.UrlPathEncode(),它用于代替空格.

为什么有两种处理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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读