asp.net core mvc中json返回首字母大小写问题

在 Startup 类里的 ConfigureServices 方法里进行配置,只需一行代码

    public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc()
                    .AddJsonOptions(opt => {
                    opt.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
                });
            }

DefaultContractResolver 是原样输出,后台属性怎么写的,返回的 json 就是怎样的。

CamelCasePropertyNamesContractResolver :驼峰命名法,首字母小写。如果变量全为大写,比如:NAME,返回的是 name

如果要全部小写的,可自行实现 DefaultContractResolver

    public class LowercaseContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
        {
            protected override string ResolvePropertyName(string propertyName)
            {
                return propertyName.ToLower();
            }
        }
---------------------
作者:csdn296
来源:CSDN
原文:https://blog.csdn.net/csdn296/article/details/87723438
版权声明:本文为博主原创文章,转载请附上博文链接!

posted on 2019-06-25 17:17  4job  阅读(800)  评论(0编辑  收藏  举报

导航