Escape text for HTML

后端 未结 9 890
别那么骄傲
别那么骄傲 2020-11-28 04:50

How do i escape text for html use in C#? I want to do

sample=\"blah\"

and have

blah&l         


        
9条回答
  •  独厮守ぢ
    2020-11-28 05:15

    If you're using .NET 4 or above and you don't want to reference System.Web, you can use WebUtility.HtmlEncode from System

    var encoded = WebUtility.HtmlEncode(unencoded);
    

    This has the same effect as HttpUtility.HtmlEncode and should be preferred over System.Security.SecurityElement.Escape.

提交回复
热议问题