解決asp.net core在輸出中文時亂碼的問題

高洛峰
發布: 2017-02-07 11:57:28
原創
1924 人瀏覽過

前言

作為一個.NET Web開發者,我最傷心的時候就是專案開發部署時面對Windows Server上貧瘠的解決方案,同樣是神器Nginx,Win上的Nginx便始終不如Linux上的,你或許會說“幹嘛不用windows自帶的NLB呢”,那這就是我這個小鳥的從眾心理了,君不見Stack Overflow 2016最新架構中,用的負載和緩存技術也都是採用在Linux上已經成熟的解決方案嗎。沒辦法的時候找個適合的解決方法是好事,有辦法的時候當然要選擇最好的解決方法。

所幸,.ASP.NET Core出現了,它順應了開源大趨勢,擺脫了一直為人詬病的Win Server,以ASP.NET的跨平台版本出現在了我們的眼前。暫且不論Benchmark中無聊的效能比較,也不探討將來是否能和JAVA,PHP Web應用分庭抗禮,但是至少對我們.NET平台開發者來說,我們多了一種開發方向,也多了一個嘗試前沿成熟技術的機會。下面話不多說了,本文主要介紹的是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=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("您好,北京欢迎你");
});
}
}
登入後複製

解决asp.net core在输出中文时乱码的问题

那麼我們取得「GB2312」編碼,然後對其編碼呢?

public static void Main(string[] args)
{
Console.WriteLine("您好,北京欢迎你");
try
{
Console.WriteLine(Encoding.GetEncoding("GB2312"));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
}
登入後複製

解决asp.net core在输出中文时乱码的问题

'GB2312' is not a supported encoding name. For information on defining a custom encoding, see the documentation for information on defining a custom encoding, see the documentation for information on defining a custom encoding, see the documentation for thenidodion 的意思是上面。 coding 不支援GB2312編碼,需要使用Encoding.RegisterProvider方法進行註冊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"
}
}
},
登入後複製
   

,我們修改了頁面,我們修改頁上輸出,或是在控制台輸出中文的時候,要注意進行註冊Provider。以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或工作能帶來一定的幫助,如果有疑問大家可以留言交流。

更多解決asp.net core在輸出中文時亂碼的問題相關文章請關注PHP中文網!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!