Which Uri Encoding method should i use in C#/.net?

This too is one of the boring "factual" posts. Sorry Lachlan.

I never know which .net uri encoding (or url encoding?) method to use in any given scenario.

So I've built this informative lookup table you can use, whenever you're wondering what sort of encoding to apply to a string that is part of, or all of, a URL. Or URI, url, Url, Uri, or uri.

(This is exactly the sort of fun stuff you need to think about constantly when writing a brilliant and much-loved tool like NimbleText, so that hopefully, sometimes, just sometimes less other people have to worry about it.)

uri_encoding.png

↑ That was a picture, which you can download and print out. ↓ This is a HTML table which you can download, view source and edit.

Character Url.Encode(s) HttpUtility.UrlEncode(s) HttpUtility.UrlPathEncode(s) Uri.EscapeDataString(s) Uri.EscapeUriString(s)
{space} + + %20 %20 %20
{tab} %09 %09 %09 %09 %09
{\r} %0d %0d %0d %0D %0D
{\n} %0a %0a %0a %0A %0A
{\0} %00 %00 %00 %00 %00
~ %7e %7e ~ ~ ~
! ! ! ! ! !
@ %40 %40 @ %40 @
# %23 %23 # %23 #
$ %24 %24 $ %24 $
% %25 %25 % %25 %25
^ %5e %5e ^ %5E %5E
& %26 %26 & %26 &
* * * * * *
( ( ( ( ( (
) ) ) ) ) )
_ _ _ _ _ _
+ %2b %2b + %2B +
{ %7b %7b { %7B %7B
} %7d %7d } %7D %7D
| %7c %7c | %7C %7C
: %3a %3a : %3A :
" %22 %22 " %22 %22
< %3c %3c < %3C %3C
> %3e %3e > %3E %3E
? %3f %3f ? %3F ?
` %60 %60 ` %60 %60
[ %5b %5b [ %5B %5B
] %5d %5d ] %5D %5D
\ %5c %5c \ %5C %5C
; %3b %3b ; %3B ;
' %27 %27 ' ' '
, %2c %2c , %2C ,
. . . . . .
/ %2f %2f / %2F /
' %27 %27 ' ' '

Remember kids: Cool URIs don't change. People do.

 

I'm currently writing a book about how to build your first product. If you want to build your first product, please sign up to be notified when the book is available.

(By the way, I read every comment and often respond.)

Your comment, please?

Your Name
Your Url (optional)
Note: I may edit, reuse or delete your comment. Don't be mean.