這篇文章主要介紹“怎么解決asp.net core在輸出中文時(shí)亂碼的問題”,在日常操作中,相信很多人在怎么解決asp.net core在輸出中文時(shí)亂碼的問題問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么解決asp.net core在輸出中文時(shí)亂碼的問題”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)公司基于成都重慶香港及美國等地區(qū)分布式IDC機(jī)房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動(dòng)大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)溫江服務(wù)器托管報(bào)價(jià),主機(jī)托管價(jià)格性價(jià)比高,為金融證券行業(yè)服務(wù)器托管,ai人工智能服務(wù)器托管提供bgp線路100M獨(dú)享,G口帶寬及機(jī)柜租用的專業(yè)成都idc公司。問題重現(xiàn)
新建控制臺和站點(diǎn)
public class Program { public static void Main(string[] args) { Console.WriteLine("您好,北京歡迎你"); Console.Read(); } }
站點(diǎn)
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("您好,北京歡迎你"); }); } }
那么我們獲取“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
方法進(jìn)行注冊Provider。
try { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); Console.WriteLine(Encoding.GetEncoding("GB2312")); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read();
CodePagesEncodingProvider在包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); }
總結(jié)
所以在頁面上輸出,或者在控制臺輸出中文的時(shí)候,要注意進(jìn)行注冊Provider。
到此,關(guān)于“怎么解決asp.net core在輸出中文時(shí)亂碼的問題”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!