Last Updated: February 25, 2016
·
762
· shin1103

How to change .NET HTML encoding

.NET Framework 4.0 or later, able to change .NET HTML encoding By httpRuntime attribute in web.config.

httpRuntime has a attribute to change HttpEncoder type. This attribute is "encoderType".
To customize encoding, set a class that inherits from HttpEncoder class.

web.config setting

< httpRuntime encoderType="WebApplication1.CustomHttpEncoder"/>

customized class

namespace WebApplication1

public class CustomHttpEncoder : System.Web.Util.HttpEncoder
{
protected override void HtmlAttributeEncode(string value, System.IO.TextWriter output)
{
//Write custom behavior ( Attribute behavior)
}

protected override void HtmlEncode(string value, System.IO.TextWriter output)
{
//Write custom behavior ( Element behavior)
}

}