Server.UrlEncode()方法 空格转换成了+而非%20
2024-10-19 02:20:29
在ASP.NET MVC 的Control类里提供了该方法。该方法可以很方便的对字符串进行url编码,但小猪今天却发现其将空格编码后变成了“+”而非JavaScript采用的encodeURIComponent()编码之后的%20。也许这算一个bug也许也不算。仔细想想在我们的url中确实不会存在空格,但是文件系统的命名却是可以使用空格的(Program Files),所以必须将空格转码。
那为什么在.Net下回转换成+而在js中会是%20呢?关键问题是在encode成+之后再decode却不能转换成了空格了呀。这确实是个蛋疼的问题。引用了老外的一段描述:
As far as I know, historically the "+" has been used in URL encoding as a special substitution for the space char ( ASCII 20 ). If an implementation does not take the space into consideration as a special character with the '+' substitution, then it still has to escape it using its ASCII code ( hence '%20' )
Actually they're both wrong! JavaScript escape() should never be used. As well as failing to encode the + character to %2B, it encodes all non-ASCII characters as a non-standard %uNNNN sequence.
Meanwhile Server.UrlEncode is not exactly URL-encoding as such, but encoding to application/x-www-form-urlencoded, which should only normally be used for query parameters. Using + to represent a space outside of a form name=value construct, such as in a path part, is wrong.
This is rather unfortunate. You might want to try doing a string replace of the + character with %20 after encoding with UrlEncode() when you are encoding into a path part rather than a parameter. In a parameter, + and %20 are equally good.
最新文章
- .NET Core采用的全新配置系统[7]: 将配置保存在数据库中
- android Content Provider介绍
- canvas简介
- text()和html()的区别,以及val()
- linux命令学习使用记录
- HoG
- web前端基础——补充
- HDU 5792 World is Exploding 树状数组+枚举
- php的一些简单算法程序(冒泡、快速等)
- R语言快速入门
- python del函数
- -webkit-text-size-adjust: none;该如何处理
- phpMyAdmin 中数据库替换问题
- Unix/Linux环境C编程新手教程(41) C语言库函数的文件操作具体解释
- Jdbc访问数据库篇
- python基础1之 由来、种类、优缺点、安装环境
- 高手养成计划基础篇-Linux第二季
- Java NIO2:NIO概述
- 2018年全国多校算法寒假训练营练习比赛(第一场)C 六子冲
- 内存区划分、内存分配、常量存储区、堆、栈、自由存储区、全局区[C++][内存管理][转载]