在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. sdutoj 2610 Boring Counting

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2610 Boring Counting Time ...

  2. ExtJs Grid 删除,编辑,查看详细等超链接处理

    在网上查了好多资料,关于ExtJs处理操作栏的“删除”.“编辑”.“查看详细”的处理,原来项目都是这么处理: 操作栏:{ text:'操作', xtype:'actioncolumn', items ...

  3. python核心编程学习记录之错误与异常

  4. Python基础第三篇

    一.collections系列 Counter是对字典类型的补充,用于追踪值的出现次数,具备字典的所有功能 + 自己的功能 1.计数器Counter import collections a='aba ...

  5. LA 5135 井下矿工

    题目链接:http://vjudge.net/contest/141787#problem/B 白书P318 题目大意:有N个矿井 ,由一些隧道连接起来,现在要修建尽量少的安全通道,使得无论哪里发生事 ...

  6. Flowplayer-Subtitle

    SOURCE URL: https://flowplayer.org/docs/subtitles.html Setting up Subtitles are loaded with a <tr ...

  7. struct内存对齐1:gcc与VC的差别

    struct内存对齐:gcc与VC的差别 内存对齐是编译器为了便于CPU快速访问而采用的一项技术,对于不同的编译器有不同的处理方法. Win32平台下的微软VC编译器在默认情况下采用如下的对齐规则:  ...

  8. Oracle软件开发分析

    软件开发的步骤可大致分为: 1.需求分析 2.系统设计 3.编码实现 4.系统测试 5.运行维护 student class     多对一 sno name age gender cid id  c ...

  9. 汇编中push寄存器的影响

    由于汇编代码中的不同位置 ESP+40所指的地址不像EBP+40的地址那个是一定的,ESP+40指向的地址由栈顶变化影响.也就是和操作栈指令 PUSH POP 等影响. 汇编栈是一个线性的地址区间,地 ...

  10. 通过HWND获得CWnd指针

    cwnd 又为计算机网络中拥塞窗口(congestion window)的简写.拥塞窗口的大小取决于网络的拥塞程度,并且动态地在变化.发送方让自己的发送窗口还可能小于拥塞窗口. CWnd是MFC窗口类 ...