126

I need to decode HTML characters in .NET Core (MVC6). It looks like .NET Core doesn't have WebUtility.HtmlDecode function which everybody used for that purpose before. Is there a replacement exist in .NET Core?

4
  • 1
    Take a look: msdn.microsoft.com/library/73z22y6h%28v=vs.100%29.aspx Commented Feb 16, 2016 at 16:15
  • 2
    @duDE, he is asking .NET Core rather than .NET 4. Commented Feb 16, 2016 at 16:16
  • Take a look at my answer. it is replacement of webutility.htmldecode in .net core as httputility.HtmlDecode . Commented Jun 4, 2019 at 9:22
  • WARNING: When searching and replacing be careful not to confuse HtmlEncode and UrlEncode especially if you're changing many at once. One is for HTML tags and the other for URLs. I almost made that silly mistake when searching and replacing! Commented Jan 20, 2023 at 20:23

6 Answers 6

142

This is in the System.Net.WebUtility class (Since .NET Standard 1.0) :

// // Summary: // Provides methods for encoding and decoding URLs when processing Web requests. public static class WebUtility { public static string HtmlDecode(string value); public static string HtmlEncode(string value); public static string UrlDecode(string encodedValue); public static byte[] UrlDecodeToBytes(byte[] encodedValue, int offset, int count); public static string UrlEncode(string value); public static byte[] UrlEncodeToBytes(byte[] value, int offset, int count); } 
Sign up to request clarification or add additional context in comments.

3 Comments

For .NET Core 2.1 see Gerardo's response below, no need to install another nuget package.
52

This is in .NET Core 2.0

using System.Text.Encodings.Web; 

and call it:

$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(link)}'>clicking here</a>."); 

UPDATE: Also in .NET Core 2.1:

using System.Web; HttpUtility.UrlEncode(code) HttpUtility.UrlDecode(code) 

UPDATE: Also works from .NET Core 3.1 up to .NET 5 and .NET 6.

2 Comments

There are also HttpUtility.HtmlEncode and HttpUtility.HtmlDecode methods.
@xhafan Since ASP.NET 4.0 in 2010 those methods just call System.Net.WebUtility.HtmlEncode (either directly, in 4.0, or via HttpEncoder in 4.5).
26

I've found the HtmlDecode function in the WebUtility library to work.

System.Net.WebUtility.HtmlDecode(string) 

Comments

6

You need add reference System.Net.WebUtility.

  • It's already included in .Net Core 2 (Microsoft.AspNetCore.All)

  • Or you can install from NuGet - preview version for .Net Core 1.

So example, your code will be look like the below

public static string HtmlDecode(this string value) { value = System.Net.WebUtility.HtmlDecode(value); return value; } 

1 Comment

Or just call WebUtility.HtmlDecode there's no reason to wrap it in an extension method...
4
namespace System.Web { // // Summary: // Provides methods for encoding and decoding URLs when processing Web requests. // This class cannot be inherited. public sealed class HttpUtility { public HttpUtility(); public static string HtmlAttributeEncode(string s); public static void HtmlAttributeEncode(string s, TextWriter output); public static string HtmlDecode(string s); public static void HtmlDecode(string s, TextWriter output); public static string HtmlEncode(string s); public static string HtmlEncode(object value); public static void HtmlEncode(string s, TextWriter output); public static string JavaScriptStringEncode(string value); public static string JavaScriptStringEncode(string value, bool addDoubleQuotes); public static NameValueCollection ParseQueryString(string query); public static NameValueCollection ParseQueryString(string query, Encoding encoding); public static string UrlDecode(string str, Encoding e); public static string UrlDecode(byte[] bytes, int offset, int count, Encoding e); public static string UrlDecode(string str); public static string UrlDecode(byte[] bytes, Encoding e); public static byte[] UrlDecodeToBytes(byte[] bytes, int offset, int count); public static byte[] UrlDecodeToBytes(string str, Encoding e); public static byte[] UrlDecodeToBytes(byte[] bytes); public static byte[] UrlDecodeToBytes(string str); public static string UrlEncode(string str); public static string UrlEncode(string str, Encoding e); public static string UrlEncode(byte[] bytes); public static string UrlEncode(byte[] bytes, int offset, int count); public static byte[] UrlEncodeToBytes(string str); public static byte[] UrlEncodeToBytes(byte[] bytes); public static byte[] UrlEncodeToBytes(string str, Encoding e); public static byte[] UrlEncodeToBytes(byte[] bytes, int offset, int count); [Obsolete("This method produces non-standards-compliant output and has interoperability issues. The preferred alternative is UrlEncode(String).")] public static string UrlEncodeUnicode(string str); [Obsolete("This method produces non-standards-compliant output and has interoperability issues. The preferred alternative is UrlEncodeToBytes(String).")] public static byte[] UrlEncodeUnicodeToBytes(string str); public static string UrlPathEncode(string str); } } 

You can use HttpUtility class in .net core for decoding or encoding.

hope it will work.

1 Comment

This answer only applies to .NET Framework, .NET Core 3.1 and .NET 5 (and later), other versions of .NET (such as .NET Core 1.x and 2.x) did not have System.Web.HttpUtility: learn.microsoft.com/en-us/dotnet/api/…
2

HtmlDecode and most of the *Decode methods were not ported to CoreFx. Only the *Encode methods are available.

Here's what's available today: https://github.com/dotnet/corefx/blob/1dfe38aeb2811fbbd6d4de36d210f060e80d50a6/src/System.Text.Encodings.Web/src/System/Text/Encodings/Web/HtmlEncoder.cs

1 Comment

This answer is from 2016 and is no-longer applicable to .NET Core 3.1 or later.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.