Why is the comma URL encoded?

前端 未结 4 1974
暖寄归人
暖寄归人 2020-12-09 08:14

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%2Cb         


        
相关标签:
4条回答
  • 2020-12-09 08:52

    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.

    0 讨论(0)
  • 2020-12-09 08:57

    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

    ! * ' ( ) ; : @ & = + $ , / ? % # [ ]
    
    0 讨论(0)
  • 2020-12-09 09:05

    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.

    0 讨论(0)
  • 2020-12-09 09:10

    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.

    0 讨论(0)
提交回复
热议问题