How to encode query string space with %20 instead of + ?
Because System.Web HttpUtility.UrlEncode() gives the space with +.
-
In modern .Net Core UrlEncode uses %20Michael Freidgeim– Michael Freidgeim2020-02-19 21:46:05 +00:00Commented Feb 19, 2020 at 21:46
-
10@MichaelFreidgeim might you be a little more specific than "modern" please - I am running with target framework netcoreapp3.1 and HttpUtility.UrlEncode(somestring) is giving me + instead of %20MemeDeveloper– MemeDeveloper2021-01-19 20:37:53 +00:00Commented Jan 19, 2021 at 20:37
-
7.Net core 6 still gives + instead of %20Louis Somers– Louis Somers2023-07-07 09:56:14 +00:00Commented Jul 7, 2023 at 9:56
Add a comment
|
1 Answer
You can use System.Uri.EscapeDataString:
var encoded = Uri.EscapeDataString("How to encode");
OUTPUT:
How%20to%20encode
1 Comment
arrakis90
Uri.EscapeUriString has been marked as obsolete as of 9/15/2021 since it "can corrupt the Uri string in some cases." As @Robert Koernke said, the new recommended approach by Microsoft is Uri.EscapeDataString()