Why is the comma URL encoded?

亡梦爱人 提交于 2019-12-18 12:52:54

问题


When debugging in ASP.NET MVC, I don't see a difference between:

http://mysite.com?q=hi,bye

and

http://mysite.com?q=hi%2Cbye

The querystring param "q" always has a value of "hi,bye".

So why is the comma encoded?

I want to do something like this 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?


回答1:


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-delims such 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.




回答2:


I found this list of characters that do not require URL encoding: 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

List of allowed URL characters

Unreserved - May be encoded but it is not necessary

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

! * ' ( ) ; : @ & = + $ , / ? % # [ ]



回答3:


This is really browser dependent. The browser takes the HTML form and decides how to build the URL based on the form's inputs.

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.

If you want to prevent the comma from being encoded for all browsers, you would have to use JavaScript and build the URL yourself.

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




回答4:


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

ASP.NET automatically encodes and decodes all required characters like this so u need not worry about them.



来源:https://stackoverflow.com/questions/8828702/why-is-the-comma-url-encoded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!