微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

在最近的一个项目中,我有幸排除了一个错误,该错误涉及当文件名中有空格时图片无法加载.我想“这是一个简单的问题,我会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.

原文地址:https://www.jb51.cc/aspnet/251309.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐