Convert Special characters into Html Encoded characters

吃可爱长大的小学妹 提交于 2020-05-11 13:09:45

问题


I want to converter all special characters into Html encoded characters. I found many post related to used HttpUtility.HtmlEncode(); , but it's only convert some of special characters like "&", "<", ">".

Is there any way to convert all special character like "š","Ø","þ","›","Ù" into Html entity using C# or javascript?


回答1:


The Microsoft AntiXss Library can accomplish this;

string p = Microsoft.Security.Application.Encoder.HtmlEncode("aaa <b>sdf</b> š,Ø,þ,›,Ù", true);
Response.Write(p);

For

aaa &lt;b&gt;sdf&lt;/b&gt; &scaron;,&Oslash;,&thorn;,&rsaquo;,&Ugrave;



回答2:


you can also do as follows without AntiXSS

public static string HtmlEncode (string text)
{
    string result;
    using (StringWriter sw = new StringWriter())
    {
        var x = new HtmlTextWriter(sw);
        x.WriteEncodedText(text);
        result = sw.ToString();
    }
    return result;

}



回答3:


Yes.Using javascript Escape them.

document.write(escape("3423424242<><><$$"));


来源:https://stackoverflow.com/questions/17106949/convert-special-characters-into-html-encoded-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!