我有用HTML字符实体编码的电子邮件地址。.NET中有什么东西可以将它们转换为普通字符串吗?
当前回答
正如@CQ所说,您需要使用HttpUtility。HtmlDecode,但是默认情况下它在非asp . net项目中不可用。
对于非asp . net应用程序,需要添加对System.Web.dll的引用。在解决方案资源管理器中右键单击项目,选择“添加引用”,然后浏览列表中的System.Web.dll。
现在已经添加了引用,您应该能够使用完全限定名System. web . httputiliti . htmldecode访问方法,或者为System插入using语句。Web使事情变得更容易。
其他回答
在。net 4.0上:
System.Net.WebUtility.HtmlDecode()
c#项目不需要包含程序集
使用服务器。HtmlDecode解码HTML实体。如果你想转义HTML,即显示<和>字符给用户,使用Server.HtmlEncode。
你可以使用HttpUtility。HtmlDecode
如果你使用的是。net 4.0+,你也可以使用WebUtility。HtmlDecode不需要额外的程序集引用,因为它在系统中可用。网络名称空间。
将static方法写入某个实用程序类,该实用程序类接受string作为参数并返回解码后的html字符串。
在你的类中包含使用System.Web.HttpUtility
public static string HtmlEncode(string text)
{
if(text.length > 0){
return HttpUtility.HtmlDecode(text);
}else{
return text;
}
}
对于。net 4.0
在项目中添加System.net.dll的引用,使用System.Net;然后使用以下扩展
// Html encode/decode
public static string HtmDecode(this string htmlEncodedString)
{
if(htmlEncodedString.Length > 0)
{
return System.Net.WebUtility.HtmlDecode(htmlEncodedString);
}
else
{
return htmlEncodedString;
}
}
public static string HtmEncode(this string htmlDecodedString)
{
if(htmlDecodedString.Length > 0)
{
return System.Net.WebUtility.HtmlEncode(htmlDecodedString);
}
else
{
return htmlDecodedString;
}
}
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何分裂()一个分隔字符串到一个列表<字符串>
- 如何转换列表<字符串>列表<int>?
- c#对象列表,我如何得到一个属性的和