asp.net-mvc 为什么要对逗号 URL 进行编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8828702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is the comma URL encoded?
提问by Scott Coates
When debugging in ASP.NET MVC, I don't see a difference between:
在 ASP.NET MVC 中调试时,我看不出以下区别:
http://mysite.com?q=hi,bye
and
和
http://mysite.com?q=hi%2Cbye
The querystring param "q" always has a value of "hi,bye".
查询字符串参数“q”的值始终为“hi,bye”。
So why is the comma encoded?
那么为什么要对逗号进行编码呢?
I want to do something like this https://stackoverflow.com/a/752109/173957.
我想做这样的事情https://stackoverflow.com/a/752109/173957。
I have this form:
我有这个表格:
<form method="GET" action="/Search">
<input type="hidden" name="q" value="hi,bye"/>
<input type="submit" value="ok"/>
</form>
How can I prevent this value from being encoded?
我怎样才能防止这个值被编码?
采纳答案by Kyle Jones
The URI spec, RFC 3986, specifies that URI path components not contain unencoded reserved characters and comma is one of the reserved characters. For sub-delimssuch as the comma, leaving it unencoded risks the character being treated as separator syntax in the URI scheme. Percent-encoding it guarantees the character will be passed through as data.
URI 规范RFC 3986指定 URI 路径组件不包含未编码的保留字符,逗号是保留字符之一。对于子分隔符(例如逗号),将其保留为未编码的字符可能会被视为 URI 方案中的分隔符语法。百分比编码它保证字符将作为数据传递。
回答by Alex
I found this list of characters that do not requireURL encoding: http://web.archive.org/web/20131212154213/http://urldecoderonline.com/url-allowed-characters.htm
我发现这个不需要URL 编码的字符列表:http://web.archive.org/web/20131212154213/http: //urldecoderonline.com/url-allowed-characters.htm
Update
Since the original link broke, I used archive.org to get the following text from the page from on December 2013
更新
由于原始链接断开,我使用 archive.org 从 2013 年 12 月的页面中获取以下文本
List of allowed URL characters
允许的 URL 字符列表
Unreserved - May be encoded but it is not necessary
Unreserved - 可以编码,但不是必需的
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 - _ . ~
Reserved - Have to be encoded sometimes
保留 - 有时必须编码
! * ' ( ) ; : @ & = + $ , / ? % # [ ]
回答by HymanAce
This is really browser dependent. The browser takes the HTML form and decides how to build the URL based on the form's inputs.
这确实取决于浏览器。浏览器获取 HTML 表单并根据表单的输入决定如何构建 URL。
If you're using a really old (or poorly programmed) browser, it may not encode the comma. If you adhere to RFC standards, it really should be encoded.
如果您使用的是非常旧的(或编程不良的)浏览器,它可能不会对逗号进行编码。如果你遵守 RFC 标准,它真的应该被编码。
If you want to prevent the comma from being encoded for all browsers, you would have to use JavaScript and build the URL yourself.
如果要防止所有浏览器对逗号进行编码,则必须使用 JavaScript 并自己构建 URL。
<script lang="JavaScript">
document.location.href = "/Search?q=hi,bye";
</script>
In any case, it shouldn't matter, because you should be decoding the querystring parameters anyway, and the result will be the same.
在任何情况下,这都无关紧要,因为无论如何您都应该解码查询字符串参数,结果将是相同的。
回答by PC.
there are several characters that hold special meaning(like + ? # etc) or are directly not allowed(like space, comma etc) in a URL. to use such characters in a URL, u need to encode and decode them. Read more Here
有几个字符具有特殊含义(如 + ? # 等)或直接不允许(如空格、逗号等)出现在 URL 中。要在 URL 中使用此类字符,您需要对它们进行编码和解码。在这里阅读更多
ASP.NET automatically encodes and decodes all required characters like this so u need not worry about them.
ASP.NET 会像这样自动编码和解码所有必需的字符,因此您无需担心它们。

