日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

asp.net core输出中文乱码的问题

發布時間:2025/3/8 asp.net 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 asp.net core输出中文乱码的问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文:asp.net core輸出中文亂碼的問題

摘要

在學習asp.net core的時候,嘗試在控制臺,或者頁面上輸出中文,會出現亂碼的問題。

問題重現

新建控制臺和站點

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

站點

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=398940public 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("您好,北京歡迎你");});}}

那么我們獲取“GB2312”編碼,然后對其編碼呢?

public static void Main(string[] args){Console.WriteLine("您好,北京歡迎你");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

上面的大概意思是Encoding 不支持GB2312編碼,需要使用Encoding.RegisterProvider方法進行注冊Provider。

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

CodePagesEncodingProvider在Nuget包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"}}},

好了,我們修改下代碼,先注冊,然后輸出中文

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

結語

所以在頁面上輸出,或者在控制臺輸出中文的時候,要注意進行注冊Provider。

參考

https://msdn.microsoft.com/zh-cn/library/system.text.encoding.registerprovider(v=vs.110).aspx

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的asp.net core输出中文乱码的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。