Solve the problem of garbled characters in asp.net core when outputting Chinese

高洛峰
Release: 2017-02-07 11:57:28
Original
1973 people have browsed it

Preface

As a .NET Web developer, the saddest time for me is when I face the poor solution on Windows Server during project development and deployment. It is also the artifact Nginx, but Nginx on Win is always inferior. On Linux, you may say "Why not use the NLB that comes with Windows?" Then this is the herd mentality of a little bird like me. Didn't you see that the latest architecture of Stack Overflow 2016 also uses the same load and cache technology? Adopt solutions that are already mature on Linux? It is a good thing to find a suitable solution when there is no other way. Of course, you should choose the best solution when there is a way.

Fortunately, .ASP.NET Core has appeared. It conforms to the open source trend, gets rid of the Win Server that has been criticized, and appears in front of us as a cross-platform version of ASP.NET. Regardless of the boring performance comparisons in Benchmark, or whether we can compete with JAVA and PHP web applications in the future, at least for us .NET platform developers, we have one more development direction and one more cutting-edge and mature endeavor. technology opportunities. Not much to say below. This article mainly introduces the problem of garbled characters when asp.net core outputs Chinese. Let’s take a look together.

Reproduction of the problem

New console and site

public class Program
{
public static void Main(string[] args)
{ 
Console.WriteLine("您好,北京欢迎你");
Console.Read();
}
}
Copy after login

Site

public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
 
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
 
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
 
app.Run(async (context) =>
{
await context.Response.WriteAsync("您好,北京欢迎你");
});
}
}
Copy after login

解决asp.net core在输出中文时乱码的问题

Then we get "GB2312" encoding and then encoding it?

public static void Main(string[] args)
{
Console.WriteLine("您好,北京欢迎你");
try
{
Console.WriteLine(Encoding.GetEncoding("GB2312"));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
}
Copy after login

解决asp.net core在输出中文时乱码的问题

'GB2312' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
Parameter name: name

The above probably means that Encoding does not support GB2312 encoding, and you need to use the Encoding.RegisterProvider method to register the Provider.

try
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Console.WriteLine(Encoding.GetEncoding("GB2312"));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
Copy after login

CodePagesEncodingProvider is in the package System.Text.Encoding.CodePages

"System.Text.Encoding.CodePages/4.0.1": {
"type": "package",
"dependencies": {
"Microsoft.NETCore.Platforms": "1.0.1",
"System.Collections": "4.0.11",
"System.Globalization": "4.0.11",
"System.IO": "4.1.0",
"System.Reflection": "4.1.0",
"System.Resources.ResourceManager": "4.0.1",
"System.Runtime": "4.1.0",
"System.Runtime.Extensions": "4.1.0",
"System.Runtime.Handles": "4.0.1",
"System.Runtime.InteropServices": "4.1.0",
"System.Text.Encoding": "4.0.11",
"System.Threading": "4.0.11"
},
"compile": {
"ref/netstandard1.3/System.Text.Encoding.CodePages.dll": {}
},
"runtimeTargets": {
"runtimes/unix/lib/netstandard1.3/System.Text.Encoding.CodePages.dll": {
"assetType": "runtime",
"rid": "unix"
},
"runtimes/win/lib/netstandard1.3/System.Text.Encoding.CodePages.dll": {
"assetType": "runtime",
"rid": "win"
}
}
},
Copy after login

Okay, let’s modify the code , register first, and then output Chinese

try
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Console.WriteLine(Encoding.GetEncoding("GB2312"));
Console.WriteLine("您好,北京欢迎你");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Copy after login

Summary

So when outputting on the page or outputting Chinese on the console, pay attention to registering the Provider. The above is the entire content of this article. I hope the content of this article can be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate.

For more related articles on solving the problem of garbled characters in asp.net core when outputting Chinese, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!