Web Api 参数绑定不一致
Web Api Parameter binding Inconsistent
经过几个小时的谷歌搜索并试图解决这个问题,我仍然无法解决这个问题。问题是创建方法的“[FromBody] 路由输入”参数出于某种原因将 Route.RouteMember[0].Lab 绑定到提供的数据,但它会设置 Route.RouteMember[0]。 RouteMemberType 为空。 (这将发生在 RouteMember 数组中的所有项,而不仅仅是第一项)为什么 RouteMemberType 没有绑定到输入,但输入却绑定到 lab?我必须承认,我有时看到它绑定到 RouteMemberType 但没有明显的原因。
相关WebApiConfig
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
json.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
路由控制器
[HttpPost]
public Route Create([FromBody] Route input) {
UnitOfWork.Routes.Add(input);
UnitOfWork.SaveChanges();
return input;
}
路线
public class Route : BaseModel {
public String Name { get; set; }
public String ShortName { get; set; }
public virtual ICollection<RouteMember> RouteMembers { get; set; }
}
路由成员
public class RouteMember : BaseModel {
[ForeignKey("Route")]
public int RouteId { get; set; }
public virtual Route Route { get; set; }
[ForeignKey("Lab")]
public int LabId { get; set; }
public virtual Lab Lab { get; set; }
[ForeignKey("RouteMemberType")]
public int RouteMemberTypeId { get; set; }
public virtual RouteMemberType RouteMemberType { get; set; }
}
实验室
public class Lab : BaseModel {
public String Name { get; set; }
public String ShortName { get; set; }
public String Description { get; set; }
[ForeignKey("Building")]
public int BuildingId { get; set; }
public virtual Building Building { get; set; }
}
RouteMemberType
public class RouteMemberType : BaseModel {
public String Name { get; set; }
}
请求输入
{
"id":0,
"name":"t1",
"shortName":"t1",
"routeMembers":[
{
"active":true,
"labId":11,
"lab":{
"$id":"1",
"building":null,
"name":"t1a",
"shortName":"t1a",
"description":"t1a",
"buildingId":2,
"id":11,
"active":true
},
"routeMemberTypeId":1,
"routeMemberType":{
"$id":"1",
"name":"Destintation",
"id":1,
"active":true
}
},
{
"active":true,
"labId":12,
"lab":{
"$id":"2",
"building":null,
"name":"t2a",
"shortName":"t2a",
"description":"t2a",
"buildingId":2,
"id":12,
"active":true
},
"routeMemberTypeId":2,
"routeMemberType":{
"$id":"2",
"name":"Fringe",
"id":2,
"active":true
}
}
],
"active":true
}
虽然我仍然无法完全满意地解决这个问题,但它在某些方面似乎与
json.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
在 WebApiConfig 中。关闭对象引用,允许绑定 RouteMemberTypes。为解决此问题,我使用 PreserveReferencesHandling.None 并手动执行参照完整性。如果我找到更好的解决方案,我会更新
经过几个小时的谷歌搜索并试图解决这个问题,我仍然无法解决这个问题。问题是创建方法的“[FromBody] 路由输入”参数出于某种原因将 Route.RouteMember[0].Lab 绑定到提供的数据,但它会设置 Route.RouteMember[0]。 RouteMemberType 为空。 (这将发生在 RouteMember 数组中的所有项,而不仅仅是第一项)为什么 RouteMemberType 没有绑定到输入,但输入却绑定到 lab?我必须承认,我有时看到它绑定到 RouteMemberType 但没有明显的原因。
相关WebApiConfig
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
json.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
路由控制器
[HttpPost]
public Route Create([FromBody] Route input) {
UnitOfWork.Routes.Add(input);
UnitOfWork.SaveChanges();
return input;
}
路线
public class Route : BaseModel {
public String Name { get; set; }
public String ShortName { get; set; }
public virtual ICollection<RouteMember> RouteMembers { get; set; }
}
路由成员
public class RouteMember : BaseModel {
[ForeignKey("Route")]
public int RouteId { get; set; }
public virtual Route Route { get; set; }
[ForeignKey("Lab")]
public int LabId { get; set; }
public virtual Lab Lab { get; set; }
[ForeignKey("RouteMemberType")]
public int RouteMemberTypeId { get; set; }
public virtual RouteMemberType RouteMemberType { get; set; }
}
实验室
public class Lab : BaseModel {
public String Name { get; set; }
public String ShortName { get; set; }
public String Description { get; set; }
[ForeignKey("Building")]
public int BuildingId { get; set; }
public virtual Building Building { get; set; }
}
RouteMemberType
public class RouteMemberType : BaseModel {
public String Name { get; set; }
}
请求输入
{
"id":0,
"name":"t1",
"shortName":"t1",
"routeMembers":[
{
"active":true,
"labId":11,
"lab":{
"$id":"1",
"building":null,
"name":"t1a",
"shortName":"t1a",
"description":"t1a",
"buildingId":2,
"id":11,
"active":true
},
"routeMemberTypeId":1,
"routeMemberType":{
"$id":"1",
"name":"Destintation",
"id":1,
"active":true
}
},
{
"active":true,
"labId":12,
"lab":{
"$id":"2",
"building":null,
"name":"t2a",
"shortName":"t2a",
"description":"t2a",
"buildingId":2,
"id":12,
"active":true
},
"routeMemberTypeId":2,
"routeMemberType":{
"$id":"2",
"name":"Fringe",
"id":2,
"active":true
}
}
],
"active":true
}
虽然我仍然无法完全满意地解决这个问题,但它在某些方面似乎与
json.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
在 WebApiConfig 中。关闭对象引用,允许绑定 RouteMemberTypes。为解决此问题,我使用 PreserveReferencesHandling.None 并手动执行参照完整性。如果我找到更好的解决方案,我会更新