How to URL encode strings in C #

How can we encode a string using the URL standard (RFC 1738) in C #?

The following online tool converts strings using this standard http://www.freeformatter.com/url-encoder.html

An example of the string I want to convert is test(brackets) , and the encoded string should look like this:

 test%28brackets%29 
+8
c # encoding url-encoding
source share
2 answers

According to RFC 1738 :

 Thus, only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL. 

Neither HttpUtility.UrlEncode nor WebUtility.UrlEncode will WebUtility.UrlEncode these characters, as the standard states that brackets () can be used without restrictions.

I don’t know why the URL encoder / decoder you linked encodes them, since it also lists them as a character that can be used in the URL.

+5
source share

Uri.EscapeDataString does what you want. See MSDN .

+12
source share

All Articles