81

How to encode query string space with %20 instead of + ? Because System.Web HttpUtility.UrlEncode() gives the space with +.

3
  • In modern .Net Core UrlEncode uses %20 Commented 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 %20 Commented Jan 19, 2021 at 20:37
  • 7
    .Net core 6 still gives + instead of %20 Commented Jul 7, 2023 at 9:56

1 Answer 1

117

You can use System.Uri.EscapeDataString:

var encoded = Uri.EscapeDataString("How to encode");

OUTPUT:

How%20to%20encode
Sign up to request clarification or add additional context in comments.

1 Comment

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()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.