從今天開始,正式進入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