HTML Encode Online

HTML Encoding in C#

In C#, HTML encoding and decoding can be done by using WebUtility.HtmlEncode() and WebUtility.HtmlDecode() methods in System.Net assembly.

using System.Net;

class Program
{
    static void Main(string[] args)
    {
        string encodedText = WebUtility.HtmlEncode("<html></html>");
        string decodedText = WebUtility.HtmlDecode(encodedText);
    }
}

When using ASP.NET, we can use Server.HtmlEncode() and Server.HtmlDecode() methods in code behind. Server is an instance of System.Web.HttpServerUtility, which is provided as a property in WebForms Page or MVC Controller class. WebUtility.HtmlEncode() and Server.HtmlEncode() methods are equivalent. Server.HtmlEncode() is just handy when programming in ASP.NET.

string encodedText = Server.HtmlEncode("<html></html>");
string decodedText = Server.HtmlDecode(encodedText);

Home