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

Server.UrlEncode()方法 空格转换成了+而非%20的更多相关文章

  1. SQL Server 表字段值转换成字段名称(二)

    上次写了个比较简单的只有两个字段的例子,经要求在写个  3 个字段的示例 ,贴上来与大家共勉一下   如果你们有更好的方法,提供一下, 感激不尽. 示例如下: /*--drop table temp_ ...

  2. 提取url中参数的方法(转换成json格式)

    还是直接上代码吧. //将url中的参数获取到并抓换成json格式 function serilizeUrl(url){ var urlObject={}; //1.正则匹配是不是以?结尾 if(/\ ...

  3. Server.UrlEncode、HttpUtility.UrlDecode的区别

    Server.UrlEncode.HttpUtility.UrlDecode的区别 在对URL进行编码时,该用哪一个?这两都使用上有什么区别吗? 测试: string file="文件上(传 ...

  4. c#asp.net url 传递中文参数要使用 System.Web.HttpUtility.UrlEncode 而不能使用Server.UrlEncode

    最近网站里的参数包括中文的例如: http://www.taiba/Tag%b0%ae%c7%e9.html 已开始使用 Server.UrlEncode来做的,但发现,有一些中文在url重写的是说找 ...

  5. HttpUtility.UrlEncode与Server.UrlEncode()转码区别

    在对URL进行编码时,该用哪一个?这两都使用上有什么区别吗?测试: string file="文件上(传)篇.doc";string Server_UrlEncode=Server ...

  6. HttpUtility.UrlEncode 和Server.UrlEncode的区别,记录记录,被乱码搞晕头了。。。。。

    今天修改原来的站点,有几个session和cookies乱码问题,然后又好好看了一下关于编码这块的内容. 大概是:登录处,用session记录了一点中文内容.然后cookies也记录了一点中文内容,取 ...

  7. Server.UrlEncode与HttpUtility.UrlEncode的区别

    一.HttpUtility.UrlEncode 方法 1.public static string UrlEncode(byte[]) 将字节数组转换为已编码的 URL 字符串. 2.public s ...

  8. HttpUtility.UrlEncode()关于空格的编码问题

    因为 HttpUtility.UrlEncode 在 Encode 的时候, 将空格转换成加号"+", 在 Decode 的时候将"+"号转为空格, 但是浏览器 ...

  9. JS字符串去替换元素再转换成数组

    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11648074.html JS字符串替换不必要的元素, 然后去除多余的空格, 转换成数组: va ...

随机推荐

  1. phpstorm-----------如何激活phpstorm2016

    新版激活方法: 1.在线激活 菜单help >>>> Register 选择License Server 输入 http://idea.qinxi1992.cn/ 点击ok 2 ...

  2. IIS7 应用程序池设置成 经典 v2.0

    HTTP 错误 500.21 - Internal Server Error 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipe ...

  3. android-eclipse-phonegap 2..9以下(包含2.9)的项目配置

    1.搭建android.eclipse环境,下载phonegap 2.9包 2.新建android项目 3.拷贝phonegap-2.9.0\lib\android\cordova-2.9.0.jar ...

  4. oracle 查看某session的历史执行sql情况

    1. 查看性能最差的前100sql SELECT * FROM ( SELECT PARSING_USER_ID EXECUTIONS,SORTS,COMMAND_TYPE,DISK_READS,sq ...

  5. Hibernate 简单使用

    首先在数据库中创建相应的表,脚本如下: create table Student (id int primary key, sName ), sNO ), sex ), email )) 在Myecl ...

  6. 处理SecureCRT中使用vim出现中文乱码问题

    处理SecureCRT中使用vim出现中文乱码问题 引用原文:http://blog.chinaunix.net/uid-20639775-id-3475608.html因为cat没有问题,定位是vi ...

  7. s3c2440 移值u-boot-2016.03 第2篇 支持Nand flash启动

    1, 要求:在4K 的代码以内,完成 NOR NAND 类型判断,初始化 NAND 复制自身到 SDRAM ,重定向. 2, 在 /arch/arm/cpu/arm920t/ 文件夹里 添加一个 in ...

  8. LeetCode----8. String to Integer (atoi)(Java)

    package myAtoi8; /* * Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  9. 分布式算法系列——一致性Hash算法

    摘自:http://www.blogjava.net/hello-yun/archive/2012/10/10/389289.html

  10. [问题2015S13] 复旦高等代数 II(14级)每周一题(第十四教学周)

    [问题2015S13]  设 \(A=(a_{ij})\) 为 \(n\) 阶实矩阵, 定义函数 \[f(A)=\sum_{i,j=1}^na_{ij}^2.\] 设 \(P\) 为 \(n\) 阶非 ...