200
HttpContext.Current.Server.UrlEncode

This does only work in .NET Framework. How can I encode or decode URI arguments in ASP.NET Core?

janw
  • 8,758
  • 11
  • 40
  • 62
wtf512
  • 4,487
  • 9
  • 33
  • 55
  • 3
    There is no HttpContext in ASP.NET Core - or any non-Web project. The same method is available through the methods of the Uri class, eg [Uri.EscapeDataString](https://msdn.microsoft.com/en-us/library/system.uri.escapedatastring(v=vs.110).aspx), [Uri.EscapeUriString](https://msdn.microsoft.com/en-us/library/system.uri.escapeuristring(v=vs.110).aspx) – Panagiotis Kanavos Jul 05 '17 at 08:55
  • 2
    @PanagiotisKanavos WRONG - There is no `HttpContext.Current` but `HttpContext` is a part of .Net Core - `Microsoft.AspNetCore.Http.HttpContext`. Remember this – J. Doe Jul 05 '17 at 12:36
  • 1
    Remember to read the entire comment. The HttpContext you mention is *VERY* different from the HttpContext of previous versions. It's *far more common* to use the Uri methods – Panagiotis Kanavos Jul 05 '17 at 12:37
  • Possible duplicate of [WebUtility.HtmlDecode replacement in .NET Core](https://stackoverflow.com/questions/35437491/webutility-htmldecode-replacement-in-net-core) – Win Jul 07 '17 at 19:17

6 Answers6

302
  • For ASP.NET Core 2.0+ just add System.Net namespace - WebUtility class is shipped as part of System.Runtime.Extensions nuget package, that is referenced by default in ASP.NET Core project.

  • For the previous version add Microsoft.AspNetCore.WebUtilities nuget package.

Then the WebUtility class will be available for you:

public static class WebUtility
{
    public static string UrlDecode(string encodedValue);
    public static string UrlEncode(string value);
}
Set
  • 47,577
  • 22
  • 132
  • 150
105

For ASP.Net Core 2.0+ and if you need spaces to be encoded as %20

as opposed to +;

Use:

 Uri.EscapeDataString(someString);
ttugates
  • 5,818
  • 3
  • 44
  • 54
100

It's available on version 2.0.0 of the .Net Core SDK, in System.Net.WebUtility.UrlEncode (see documentation)

Manuel Alves
  • 3,885
  • 2
  • 30
  • 24
  • 21
    For Net Core 2+, when I use `System.Net.WebUtility.UrlEncode`: spaces are encoded with a `+`. I used `Uri.EscapeDataString` to encode with `%20`. – ttugates Jun 04 '18 at 13:55
15

Just adding another approach to the mix:

UrlEncoder.Default.Encode(url);

https://learn.microsoft.com/en-us/dotnet/api/system.text.encodings.web.urlencoder?view=net-6.0

Michael
  • 11,571
  • 4
  • 63
  • 61
7

I'm using a redirect, and UrlEncode did not work for me because it encodes the entire url. I solved this by instead using UriHelper.Encode, shown below.

UriHelper.Encode

// generate url string...
return Redirect(Microsoft.AspNetCore.Http.Extensions.UriHelper.Encode(new System.Uri(url)));
Jordan Ryder
  • 2,336
  • 1
  • 24
  • 29
-6

Don't waste your time, I've got plenty of experience with these so called url encoders, they are all useless, and have different quirks. Eg WebUtility.UrlEncode doesn't take care of "+" sign.

If you want to encode URL parameters, employ a BASE58 encoding. It uses only alphabet letters + numbers, thus you don't need to url encode.

Alex Angas
  • 59,219
  • 41
  • 137
  • 210
Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
  • Can you explain all these quirks, I'm only familiar with + that QueryHelpers.AddQueryString or Uri.EscapeDataString doesn't do. – Michael Nov 21 '18 at 01:06
  • 3
    I've just tried out `WebUtility.UrlEncode` and it wonderfully converted a plus sign ("+") into a "%2B". I'm using .NET Core 3.1. – Dejan Jun 26 '20 at 13:38
  • 1
    @Michael Uri.EscapeDataString does convert "+" to %2B with .NET 6 – Bloggrammer Mar 09 '22 at 09:33
  • @Blogrammer True, think I meant Uri.EscapeUriString. – Michael Mar 29 '22 at 02:21
  • 1
    Here's a website covering the different output from the various methods https://secretgeek.net/uri_enconding I suggest sticking with Uri.EscapeDataString or UrlEncoder.Encode. – Michael Mar 29 '22 at 02:39
  • This is actually true. I'm using a program that uses WebUtility.UrlEncode and it broke the URL. – DxTx Feb 18 '23 at 23:06