我有2个项目(差不多)相同。我将代码从第一个复制/粘贴到第二个。所有这些都是ASP.NET MVC解决方案。在这些项目中,我检索如下数据:
$.get("/api/mail", { folder: this.params.folder }, self.chosenFolderData);
或
$.get("/api/vehicle", { category: this.params.category }, self.chosenCategoryData);
当我检查从第一个返回的json数据时,我得到了这个:
{"Folder":"Inbox","Mails":[{"MailId":4,"Folder":"Inbox","From":"XLN <basia@framehome.xyz>","To":"steve@example.com","Subject"...
正如您所看到的,我的键是大写的:文件夹,邮件,文件夹,收件箱,......
这是第二个项目返回的相同类型的json:
{"category":"Car","vehicles":[{"vehicleId":1,"category":"Car","model":"C220","brand":"Mercedes","registration":"AZE-123"}]}
我的所有钥匙都以小写字母返回:类别,车辆,型号,品牌......
知道为什么吗?
UPDATE_的 _ __ _ ___
根据要求,以下是名为:
的Web API操作 // GET api/Vehicle
public GetVehiclesViewModel GetVehicles(string category)
{
GetVehiclesViewModel viewModel = new GetVehiclesViewModel
{
Category = category,
Vehicles = db.Vehicles.Where(x => x.Category == category).OrderByDescending(u => u.Model).AsEnumerable()
};
return viewModel;
}
这是viewModel:
public class GetVehiclesViewModel
{
public string Category { get; set; }
public IEnumerable<VehicleModel> Vehicles { get; set; }
}
在我的WebAPI路由中,我也有:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Use camel case for JSON data.
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
好了,我终于发现,如果我删除它:
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
然后我的财产钥匙是骆驼套。有谁能解释我为什么?