Equivalent to Javascript encodeURI?

C #, the equivalent of encodeURIComponent , is well covered in SO and elsewhere, but what about encodeURI ? Basically, I want to encode invalid only URL characters, not reserved characters like /, :etc. so

"http://www.example.com/my cool page"

will be encoded before

"http://www.example.com/my%20cool%20page"

Is there anything in .NET for this? Or is it a regular expression that I can do better?

+3
source share
4 answers

Try

Uri.EscapeUriString("http://www.mysite.com/my cool page")
+5
source

Try

Server.URLEncode(uri.ToString)
+1
source

:

HttpUtility.UrlEncode(String)

:

var url_encoded_string = HttpUtility.UrlEncode(userInput);
+1

System.Net.WebUtility.UrlEncode(string value).

0

All Articles