我已经看过how to remove $id during JSON serialization,但给出的答案似乎对我不起作用,我希望有人能弄清楚我做错了什么。
这是我的代码:
return JsonConvert.SerializeObject(target, new JsonSerializerSettings {
NullValueHandling = NullValueHandling.Include,
PreserveReferencesHandling = PreserveReferencesHandling.None,
ContractResolver = new CustomContractResolver(),
Converters = CustomConverters
});
这个输出仍然以$ id出现,但是,仅在实体框架对象上,这是设计的吗?如果是这样,有没有办法阻止Entity Framework对象上的那些$ id?
答案 0 :(得分:18)
自定义ContractResolver
设置会覆盖PreserveReferencesHandling
设置。
在DefaultContractResolver
/ IContractResolver
的实施中,添加此内容;
public override JsonContract ResolveContract(Type type) {
var contract = base.ResolveContract(type);
contract.IsReference = false;
return contract;
}
此行为与PreserveReferencesHandling.None
设置类似,没有自定义ContractResolver
。