前言
作为一个.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("您好,北京欢迎你");
});
}
}
那么我们获取“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在包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。以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
更多解决asp.net core在输出中文时乱码的问题。