相关疑难解决方法(0)

尽管将PreserveReferencesHandling设置为"None",Json.Net仍向EF对象添加$ id

我已经看过如何在JSON序列化期间删除$ id但是给出的答案对我来说似乎没有用,我希望有人能弄清楚我做错了什么.

这是我的代码:

return JsonConvert.SerializeObject(target, new JsonSerializerSettings {
    NullValueHandling = NullValueHandling.Include,
    PreserveReferencesHandling = PreserveReferencesHandling.None,
    ContractResolver = new CustomContractResolver(),
    Converters = CustomConverters
});
Run Code Online (Sandbox Code Playgroud)

这个输出仍然以$ id出现,但仅限于Entity Framework对象,这是设计的吗?如果是这样,有没有办法阻止Entity Framework对象上的那些$ id?

c# serialization entity-framework json.net

8
推荐指数
1
解决办法
4998
查看次数

.NET WebApi如何防止"$ ref":JSON的"x"输出

我正在使用Web Api和Entity Framework,我有一个名为Gift的控制器,当客户端GET是API时,我通过使用实体框架从DB获取所有礼物并将其作为JSON返回

这是我的控制器功能

    public List<Gift> Get()
    {
        return GiftService.GetIncludeAndActive();
    }
Run Code Online (Sandbox Code Playgroud)

这是服务功能

public List<Gift> GetIncludeAndActive()
{
    return dbSet.Include("GiftCategory").Where(x => x.Status == Model.Enums.GiftStatus.Active).OrderByDescending(x => x.Featured).ThenBy(x => x.Price).ToList();
}
Run Code Online (Sandbox Code Playgroud)

这是礼物模特

public class Gift
    {
        public Gift()
        {
            this.CartItems = new List<CartItem>();
        }
        public int ID { get; set; }
        public string GiftName { get; set; }
        public string Image { get; set; }
        public int Stock { get; set; }
        public int Price { get; set; }
        public string Description { get; …
Run Code Online (Sandbox Code Playgroud)

c# asp.net json asp.net-web-api asp.net-web-api2

3
推荐指数
1
解决办法
852
查看次数