201
HttpContext.Current.Server.UrlEncode

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

4
  • 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, Uri.EscapeUriString Jul 5, 2017 at 8: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 5, 2017 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 Jul 5, 2017 at 12:37
  • Possible duplicate of WebUtility.HtmlDecode replacement in .NET Core
    – Win
    Jul 7, 2017 at 19:17

6 Answers 6

303
  • 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);
}
2
  • 8
    Didn't work for me against SDK 2.0.0+, however Manuel Alves answer (System.Net.WebUility) did. Feb 13, 2018 at 16:48
  • 5
    The usage is : var decodedUrl = WebUtility.UrlDecode(encodedUrl);
    – aris
    Apr 1, 2021 at 7:27
106

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

as opposed to +;

Use:

 Uri.EscapeDataString(someString);
100

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

1
  • 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 4, 2018 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

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)));
-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.

6
  • Can you explain all these quirks, I'm only familiar with + that QueryHelpers.AddQueryString or Uri.EscapeDataString doesn't do.
    – Michael
    Nov 21, 2018 at 1: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, 2020 at 13:38
  • 1
    @Michael Uri.EscapeDataString does convert "+" to %2B with .NET 6 Mar 9, 2022 at 9:33
  • @Blogrammer True, think I meant Uri.EscapeUriString.
    – Michael
    Mar 29, 2022 at 2:21
  • 1
    Here's a website covering the different output from the various methods secretgeek.net/uri_enconding I suggest sticking with Uri.EscapeDataString or UrlEncoder.Encode.
    – Michael
    Mar 29, 2022 at 2:39

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.