【读取html中文乱码,设置GB2312提示如下】
System.ArgumentException: 'gb2312' is not a supported encoding name
[HttpGet]
public IActionResult GetHtml(string name)
{
string strhtml = "";
try
{
string projectPath = System.Environment.CurrentDirectory;
using(Stream myStream = new FileStream(projectPath + $"\\template\\{name}.html", FileMode.Open))
{
Encoding encode = System.Text.Encoding.GetEncoding("GB2312");//utf-8、gb2312
using (StreamReader myStreamReader = new StreamReader(myStream, encode))
{
strhtml = myStreamReader.ReadToEnd();
}
}
}
catch(Exception ex)
{
}
return Json(strhtml);
}
【解决方法】
nuget引用dll:System.Text.Encoding.CodePages
//直接在starup.cs启动文件里的方法添加
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
}