UrlEncode 和 HtmlEncode

UrlEncode

是将指定的字符串按URL编码规则,包括转义字符进行编码。

1 void Main()
2 {
3     string rawUrl = "http://www.demo.com?key=测试";
4     string urlEncode = System.Web.HttpUtility.UrlEncode(rawUrl);
5     urlEncode.Dump();
6 }
View Code

HtmlEncode

是将html源文件中不容许出现的字符进行编码,通常是编码以下字符:"<"、">"、"&"、"""、"'"等。

1 void Main()
2 {
3     string rawTxt = "会被转义的字符:< > & " '";
4     string htmlEncode = System.Web.HttpUtility.HtmlEncode(rawTxt);
5     htmlEncode.Dump();
6 }
View Code
原文地址:https://www.cnblogs.com/mousewheel/p/4302444.html