Solve asp.net core garbled characters when outputting Chinese characters, asp. netcore
Preface
As. NET Web developers, my most sad time is the poor solution on Windows Server during project development and deployment. It is also an artifact Nginx, and Nginx on Win is always inferior to that on Linux, you may say, "Why don't you use NLB in windows?" That's my herd mentality. You can't see the latest architecture in Stack Overflow 2016, are the load and cache technologies used in Linux mature solutions. It is a good thing to find a suitable solution when there is no way, and the best solution should be selected when there is a way.
Fortunately, the emergence of. ASP. NET Core conforms to the general trend of open source, instead of Win Server, which has been criticized all the time, and the cross-platform version of ASP. NET appears in front of us. For the time being, no matter how boring the Benchmark performance is compared, we will not discuss whether it will be able to compete with JAVA or PHP Web applications in the future, but at least for us. for NET platform developers, we have an additional development direction and an opportunity to try cutting-edge and mature technologies. I will not talk about it much below. This article mainly introduces the garbled characters of asp.net core in Chinese output. Let's take a look at it.
Problem Reproduction
Create a console and a site
Public class Program {public static void Main (string [] args) {Console. WriteLine ("Hello, Beijing Welcomes You"); Console. Read ();}}
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 ("Hello, Beijing Welcomes You ");});}}
What if we get the "GB2312" encoding and then encode it?
Public static void Main (string [] args) {Console. writeLine ("Hello, Beijing Welcomes You"); try {Console. writeLine (Encoding. getEncoding ("GB2312");} catch (Exception ex) {Console. writeLine (ex. message);} Console. read ();}}
'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 needs to be usedEncoding.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();
CodePagesEncodingProvider in the packageSystem.Text.Encoding.CodePages
Medium
"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" } } },
Now, let's modify the code, register first, and then output Chinese characters.
Try {Encoding. registerProvider (CodePagesEncodingProvider. instance); Console. writeLine (Encoding. getEncoding ("GB2312"); Console. writeLine ("Hello, Beijing Welcomes You");} catch (Exception ex) {Console. writeLine (ex. message );}
Summary
Therefore, when outputting data on the page or outputting Chinese characters on the console, you must register the Provider. The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.
Reference
Https://msdn.microsoft.com/zh-cn/library/system.text.encoding.registerprovider (v = vs.110). aspx