Asp.net Core WebApi 返回JSON自动驼峰格式化问题
从今天开始,正式进入Asp.net Core的开发,估计最近一段时间会经常写博客了,记录学些Asp.net Core中遇到的各种坑。
第一个问题:通过core编写的webapi,默认返回的json会自动格式化为驼峰样式,并没有按照具体的类名来返回,如何让其按照类名返回呢?
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddApplicationInsightsTelemetry(Configuration); services.AddMvc().AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); ; }
在Startup.cs全局配置类里面,加入json配置选项即可解决,同时不要忘记引用:using Newtonsoft.Json.Serialization;
{"Statuz":0,"ErrorMsg":"获取成功","Data":[{"Name":"xxx","MobilePhone":"15xxxx81xxx","Sex":"0","Score":-102},{"Name":"xxxxx","MobilePhone":"15xxxxx1xx1","Sex":"0","Score":-102}]}
返回值清爽了。
参考文章:http://www.cnblogs.com/zhong-ken/p/6062782.html