DEV Community

Santhosh N
Santhosh N

Posted on

HttpUtility encode vs WebUtility encode

Hello guys, Today I have learned a difference between HttpUtility.HtmlEncode and WebUtility.HtmlEncode. If you use dot net version less than 4.7 then HttpUtility.HtmlEncode encodes the regional languages. If you want to only html tags need to encode then use WebUtility.HtmlEncode.

var regionalText = "தமிழ்";
        var htmlText = "<script>alert('OK')";

        var httpUtilityRegionalText = HttpUtility.HtmlEncode(regionalText); //&#2980;&#2990;&#3007;&#2996;&#3021;
        var httpUtilityHtmlText = HttpUtility.HtmlEncode(htmlText); //&lt;script&gt;alert(&#39;OK&#39;)

        regionalText = "தமிழ்";
        htmlText = "<script>alert('OK')";

        var webUtilityRegionalText = WebUtility.HtmlEncode(regionalText); //தமிழ்
        var webUtilityHtmlText = WebUtility.HtmlEncode(htmlText); //&lt;script&gt;alert(&#39;OK&#39;)
Enter fullscreen mode Exit fullscreen mode

Latest comments (1)

Collapse
 
eliasdavis profile image
EliasDavis

You great explain in your post! i like it! Long distance vashikaran mantra