How to encode query string space with %20 instead of + ? 
Because System.Web  HttpUtility.UrlEncode() gives the space with +.
            Asked
            
        
        
            Active
            
        
            Viewed 6.1k times
        
    64
            
            
         
    
    
        GeralexGR
        
- 2,973
- 6
- 24
- 33
 
    
    
        Sharath Kumar K J
        
- 733
- 1
- 6
- 11
- 
                    In modern .Net Core UrlEncode uses %20 – Michael Freidgeim Feb 19 '20 at 21:46
- 
                    7@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 – MemeDeveloper Jan 19 '21 at 20:37
- 
                    .Net core 6 still gives + instead of %20 – Louis Somers Jul 07 '23 at 09:56
1 Answers
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
- 
                    52You 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
- 
                    5Uri.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