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}] 其实我更喜欢默认的....
原文地址:https://www.cnblogs.com/jellydong/p/10240258.html
时间: 2024-11-13 08:14:48