asp.net core 2.0 默认返回的结果格式是Json, 并使用json.net对结果默认做了camel case的转化(大概可理解为首字母小写). 

这一点与老.net web api 不一样, 原来的 asp.net web api 默认不适用任何NamingStrategy, 需要手动加上camelcase的转化.

如果非得把这个规则去掉, 那么就在configureServices里面改一下:

        public void ConfigureServices(IServiceCollection services)
        { 
            services.AddMvc()
                .AddJsonOptions(options =>
                {
                    if (options.SerializerSettings.ContractResolver is DefaultContractResolver resolver)
                    {
                        resolver.NamingStrategy = null;
                    }
                });
        }

 修改前: 

[{"id":1,"name":"牛奶","price":2.5},{"id":2,"name":"面包","price":4.5}]
修改后:
[{"Id":1,"Name":"牛奶","Price":2.5},{"Id":2,"Name":"面包","Price":4.5}]

其实我更喜欢默认的....

相关文章:

  • 2021-10-25
  • 2022-12-23
  • 2021-09-03
  • 2022-12-23
  • 2022-12-23
  • 2021-04-09
  • 2022-01-26
  • 2021-11-28
猜你喜欢
  • 2021-05-31
  • 2022-12-23
  • 2022-12-23
  • 2021-11-14
  • 2021-07-09
  • 2021-06-24
  • 2021-12-30
相关资源
相似解决方案