1、使用UrlEncode和UrlDecode编解码
#if !NETSTANDARD using System.Web; #else using System.Net; #endif using System; namespace UrlHelper { public class UrlHelper { protected string UrlEncode(string value) { var tmp = value; #if !NETSTANDARD return HttpUtility.UrlEncode(tmp, System.Text.Encoding.GetEncoding("utf-8")); #else return WebUtility.UrlEncode(tmp); #endif } public static string UrlDecode(string value) { var tmp = value; #if !NETSTANDARD return HttpUtility.UrlDecode(tmp); #else return WebUtility.UrlDecode(tmp); #endif } } }
2、使用HtmlEncode和HtmlDecode编解码
using System; using System.Net; #if !NETSTANDARD using System.Web; #else #endif namespace HtmlHelpler { public class HtmlHelpler { public static string HtmlDecode(string value) { var tmp = value; #if !NETSTANDARD return HttpUtility.HtmlDecode(tmp); #else return WebUtility.HtmlDecode(tmp); #endif } public static string HtmlEncode(string value) { var tmp = value; #if !NETSTANDARD return HttpUtility.HtmlEncode(tmp); #else return WebUtility.HtmlEncode(tmp); #endif } } }