Replacement for System.Web.HttpUtility.UrlEncode/UrlDecode ASP.NET 5

35,630

System.Runtime.Extensions defines both UrlDecode and HtmlDecode.

namespace System.Net
{
    public static partial class WebUtility
    {
        public static string HtmlDecode(string value) { return default(string); }
        public static string HtmlEncode(string value) { return default(string); }
        public static string UrlDecode(string encodedValue) { return default(string); }
        public static byte[] UrlDecodeToBytes(byte[] encodedValue, int offset, int count) { return default(byte[]); }
        public static string UrlEncode(string value) { return default(string); }
        public static byte[] UrlEncodeToBytes(byte[] value, int offset, int count) { return default(byte[]); }
    }
}

Update

While System.Runtime.Extensions defines the extension, as you can notice from it's code the actual class you need to call is System.Net.WebUtility

Option 1: System.Net.WebUtility

Currently there are no publicly made plans to include Decode in Microsoft.Framework.WebEncoders.

Usage

System.Net.WebUtility.UrlEncode(myString)
System.Net.WebUtility.UrlDecode(myString)

Option 2: System.Text.Encodings.Web.UrlEncoder

This is registered in the asp.net core service container and is injectable into your controllers etc.

Share:
35,630
bezejmeny
Author by

bezejmeny

Updated on July 05, 2022

Comments

  • bezejmeny
    bezejmeny almost 2 years

    I would like to know if there is a replacement for System.Web.HttpUtility.UrlEncode and UrlDecode.

    As I found for Encode it should be: Microsoft.Framework.WebEncoders.UrlEncoder.Default.UrlEncode.

    But I did not find UrlDecode. Is there one?

  • bezejmeny
    bezejmeny over 8 years
    Many thanks about your answer. I had other problems to solve so i get to testing only yesterday.
  • bezejmeny
    bezejmeny over 8 years
    Many thanks for your answer. It's working, but I have small problem with it. I'am trying to encode one value and I'am comparing result with my previous application in .NET 4.5. The result is a bit different. In old asp.net i had the encoded value result this: kA5viZqq23%2f4LCe4IboZ7Q%3d%3d and with the new UrlEncode the reuslt is this: kA5viZqq23%2F4LCe4IboZ7Q%3D%3D The difference is that few letters are capital. Because of this, some values like %3D is in my old version a equal sign and in new version its only written as is so %3D is written. Do you have any idea about this?
  • Mihai Dinculescu
    Mihai Dinculescu over 8 years
    That's interesting. I would encourage you to create an issue in GitHub.
  • Juan
    Juan over 8 years
    So the call is System.Net.WebUtility.HtmlDecode(stringToDecode) for example. I added this comment because I was first confuse to add the System.Runtime.Extensions which is not the case, maybe it can help to someone else.