解決asp.net core在輸出中文時(shí)亂碼的問題
前言
作為一個(gè).NET Web開發(fā)者,我最傷心的時(shí)候就是項(xiàng)目開發(fā)部署時(shí)面對(duì)Windows Server上貧瘠的解決方案,同樣是神器Nginx,Win上的Nginx便始終不如Linux上的,你或許會(huì)說“干嘛不用windows自帶的NLB呢”,那這就是我這個(gè)小鳥的從眾心理了,君不見Stack Overflow 2016最新架構(gòu)中,用的負(fù)載和緩存技術(shù)也都是采用在Linux上已經(jīng)成熟的解決方案嗎。沒辦法的時(shí)候找個(gè)適合的解決辦法是好事,有辦法的時(shí)候當(dāng)然要選擇最好的解決辦法。
所幸,.ASP.NET Core出現(xiàn)了,它順應(yīng)了開源大趨勢,擺脫了一直為人詬病的Win Server,以ASP.NET的跨平臺(tái)版本出現(xiàn)在了我們的眼前。暫且不論Benchmark中無聊的性能比較,也不探討將來是否能和JAVA,PHP Web應(yīng)用分庭抗禮,但是至少對(duì)我們.NET平臺(tái)開發(fā)者來說,我們多了一種開發(fā)方向,也多了一個(gè)嘗試前沿成熟技術(shù)的機(jī)會(huì)。下面話不多說了,本文主要介紹的是asp.net core在輸出中文時(shí)亂碼的問題,下面來一起看看吧。
問題重現(xiàn)
新建控制臺(tái)和站點(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”編碼,然后對(duì)其編碼呢?
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é)
所以在頁面上輸出,或者在控制臺(tái)輸出中文的時(shí)候,要注意進(jìn)行注冊Provider。以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
參考
https://msdn.microsoft.com/zh-cn/library/system.text.encoding.registerprovider(v=vs.110).aspx
相關(guān)文章
輕量級(jí)ORM框架Dapper應(yīng)用之Dapper支持存儲(chǔ)過程
這篇文章介紹了Dapper支持使用存儲(chǔ)過程的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
WPF數(shù)據(jù)驅(qū)動(dòng)修改綁定
這篇文章介紹了WPF數(shù)據(jù)驅(qū)動(dòng)修改綁定的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
MVC使用Log4Net進(jìn)行錯(cuò)誤日志記錄學(xué)習(xí)筆記4
這篇文章主要為大家詳細(xì)介紹了MVC使用Log4Net進(jìn)行錯(cuò)誤日志記錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Global.asax取物理路徑/取絕對(duì)路徑具體方法
本文章來給大家簡單介紹利用Global.asax取物理路徑和取絕對(duì)路徑代碼,有需要了解的朋友可參考參考2013-08-08
如此高效通用的分頁存儲(chǔ)過程是帶有sql注入漏洞的zt
通常大家都會(huì)認(rèn)為存儲(chǔ)過程可以避免sql注入的漏洞,這適用于一般的存儲(chǔ)過程,而對(duì)于通用分頁存儲(chǔ)過程是不適合的,請(qǐng)看下面的代碼和分析!2010-07-07

