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.)
↑ 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.
Next → ← PreviousMy book "Choose Your First Product" is available now.
It gives you 4 easy steps to find and validate a humble product idea.
Steve O on February 16, 2018 15:43 sez:
Thank you!
rupal on April 03, 2018 01:25 sez:
i want to encode this url .How can i achieve this
Marcel on May 03, 2018 06:10 sez:
Hey thanks a lot - exactly what I was looking for. Uri.EscapeDataString(s) was the solution for me.
Dave on June 05, 2018 07:48 sez:
Just what I was looking for!
Robin on September 26, 2018 05:18 sez:
Actually the table should now have a before .NET 4.5 and with .NET 4.5 (or later) column.. as the output has changed.
https://stackoverflow.com/questions/24962514/uri-escapedatastring-weirdness
M Singh on June 07, 2019 17:26 sez:
I tried HttpUtility.UrlEncode and Uri.EscapeDataString to encode but the characters < and > remain un-encoded. However as per your table they should have been encoded to %3c and %3e. Could you please tell me why < and > were not encoded?
as a query string parameter. When I try to read the query string parameter on the result page I get System.Web.HttpRequestValidationException with message "A potentially dangerous Request.QueryString value was detected from the client (errorMsg="") ". How can I safely pass the query string to the result page and read the same?
I need to use
Joe Phillips on August 28, 2019 12:25 sez:
You need to include the equals sign on the chart :)
Michael on March 28, 2022 22:58 sez:
Thanks for the table. Would be helpful if WebUtility.UrlEncode and UrlEncoder.Default.Encode were added as well.