Solve asp.net core garbled characters when outputting Chinese characters, asp. netcore

Source: Internet
Author: User

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.RegisterProviderMethod 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.CodePagesMedium

 "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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.