64

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

GeralexGR
  • 2,973
  • 6
  • 24
  • 33
Sharath Kumar K J
  • 733
  • 1
  • 6
  • 11

1 Answers1

97

https://msdn.microsoft.com/en-us/library/system.uri.escapeuristring(v=vs.110).aspx

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

OUTPUT:

How%20to%20encode
Louis Somers
  • 2,560
  • 3
  • 27
  • 57
L.B
  • 114,136
  • 19
  • 178
  • 224
  • 52
    You may want to use Uri.EscapeDataString() Because Uri string will get spaces but will not encode other characters. Example '#' – Robert Koernke Jun 19 '19 at 20:07
  • 5
    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() – arrakis90 Apr 04 '22 at 16:35