This article is a mirror article of machine translation, please click here to jump to the original article.

View: 36918|Reply: 2

[.NET Core] .NET Core console Chinese character garbled characters

[Copy link]
Posted on 5/17/2017 2:44:09 PM | | | |



The .NET Core console program is very simple, that is, it prompts the user to enter a URL, and then, enter the URL entered by the user, everyone will understand at a glance, but the Chinese characters are garbled,

Error causes:

'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 general meaning is that Encoding does not support GB2312 encoding, and you need to use the Encoding.RegisterProvider method to register the Provider.



How to solve it?

Solution 1:

Set the text output of the characters that are displayed by the Settings console using encoding will be applied to the corresponding console. The system locale used by the default code page console is determined.



Solution 2:

1: Use the Encoding.RegisterProvider method to register the Provider

2: CodePagesEncodingProvider in the package System.Text.Encoding.CodePages

3: nuget executes the command: Install-Package System.Text.Encoding.CodePages

4: Add Code:










Previous:Centos Installation .NET Core Environment Tutorial
Next:Run .net core console program on Linux/Centos [source code]
 Landlord| Posted on 5/17/2017 2:46:57 PM |

I would like to share my opinion, the reason for the garbled characters on the console is because the default encoding page of the Chinese windows command line is GB2312, and if you want to output Chinese, you only need to modify the coding page of the console to Unicode.
Type chcp 65001 in cmd and run the program or add a line to the program
Console.OutputEncoding = Encoding.Unicode;
The reason for the garbled web page is also because the program does not specify the encoding and uses Unicode by default, while the browser in the Chinese environment uses gbk by default. You can tell the browser to use utf8 by setting the ContentType.
In await context. Response.WriteAsync("Hello, welcome to Beijing"); Preceded by:
context. Response.ContentType = "text/plain; charset=utf-8";
However, if you display Chinese, you don't need to refer to System.Text.Encoding.CodePages without special requirements
Posted on 11/12/2022 1:40:04 PM |
Save for later use
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com