Resolving the problem of ASP. NET core garbled when outputting Chinese

Source: Internet
Author: User
Objective

As a. NET web Developer, the saddest part of my project development deployment is the poor solution on Windows Server, the same artifact Nginx,win on Linux, and you might say " Why don't you use the NLB that comes with Windows? "That's the herd mentality of my little bird, don't you see Stack Overflow 2016 the latest architecture, the load and cache technology used in Linux is already a mature solution. When there is no way to find a suitable solution is a good thing, when there are ways to choose the best solution.

Fortunately,. The emergence of ASP. NET core, which follows the trend of open source, from the people who have been criticized for the win Server, the cross-platform version of ASP appeared in our eyes. For the moment, regardless of the benchmark performance comparisons, we will not be able to compete with java,php Web apps in the future, but at least for us. NET platform developers, we have one more development direction and one more opportunity to try cutting-edge mature technology. The following words do not say much, this article is mainly about ASP. NET core in the output of Chinese garbled problem, the following to see together.

Problem recurrence

New Console and site

public class Program{public static void Main (string[] args) {Console.WriteLine ("Hello, Welcome to Beijing"); Console.read ();}}

Site

public class startup{//This method gets called by the runtime. The use of this method to add services to the container.//for more information in how to configure your application, visit http: go.microsoft.com/fwlink/? linkid=398940public void Configureservices (Iservicecollection services) {}//This method gets a 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, Welcome to Beijing");}}

So we get the "GB2312" code, 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 in defining a custom encoding, see the documentation for the Encoding.registerprovider method.
Parameter Name:name

The above means that encoding does not support GB2312 encoding and requires the Encoding.registerprovider method to register provider.

Try{encoding.registerprovider (codepagesencodingprovider.instance); Console.WriteLine (encoding.getencoding ("GB2312"));} catch (Exception ex) {Console.WriteLine (ex. Message);} Console.read ();

Codepagesencodingprovider 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 "}},

OK, let's change the code, register it, and then output the Chinese

Try{encoding.registerprovider (codepagesencodingprovider.instance); Console.WriteLine (encoding.getencoding ("GB2312")); Console.WriteLine ("Hello, Welcome to Beijing");} catch (Exception ex) {Console.WriteLine (ex. Message);}

Summarize

Therefore, the output on the page, or in the console output Chinese, you should pay attention to the registration provider. The above is the entire content of this article, I hope that the content of this article on everyone's study or work can bring certain help, if there is doubt you can message exchange.

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.