在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.

最新文章

  1. .NET Core采用的全新配置系统[7]: 将配置保存在数据库中
  2. android Content Provider介绍
  3. canvas简介
  4. text()和html()的区别,以及val()
  5. linux命令学习使用记录
  6. HoG
  7. web前端基础——补充
  8. HDU 5792 World is Exploding 树状数组+枚举
  9. php的一些简单算法程序(冒泡、快速等)
  10. R语言快速入门
  11. python del函数
  12. -webkit-text-size-adjust: none;该如何处理
  13. phpMyAdmin 中数据库替换问题
  14. Unix/Linux环境C编程新手教程(41) C语言库函数的文件操作具体解释
  15. Jdbc访问数据库篇
  16. python基础1之 由来、种类、优缺点、安装环境
  17. 高手养成计划基础篇-Linux第二季
  18. Java NIO2:NIO概述
  19. 2018年全国多校算法寒假训练营练习比赛(第一场)C 六子冲
  20. 内存区划分、内存分配、常量存储区、堆、栈、自由存储区、全局区[C++][内存管理][转载]

热门文章

  1. Scrum 项目1.0
  2. 配置Hibernate二级缓存
  3. python学习之认识字符串
  4. CentOS7 运行级别
  5. sd 卡驱动--基于高通平台
  6. [转](三)unity4.6Ugui中文教程文档-------概要-UGUI Basic Layout
  7. abort终止正在进行中的的ajax请求
  8. CUBRID学习笔记 48查询优化
  9. Sprint(第五天11.18)
  10. Scrum Meeting 10-20151216