Why is the comma URL encoded?

Scott Coates picture Scott Coates · Jan 12, 2012 · Viewed 30.9k times · Source

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?

Answer

Kyle Jones picture Kyle Jones · Jan 12, 2012

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.