[英]How to ignore navigation properties in asp.net mvc web api
I'm using ASP.NET Web API with Entity Framework but i'm facing a problem while generating the JSON for the navigation property: 我将ASP.NET Web API与Entity Framework结合使用,但是在为导航属性生成JSON时遇到问题:
I have two tables; 我有两个桌子。 Product and Category.
产品和类别。
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public virtual Category Category { get; set; }
}
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
when I generate the JSON for Product it generates the JSON category which is fine but inside the Category JSON there is another JSON pointing to JSON product so a huge JSON file is create i tried to solve this issue by removing virtual but every time i update the model i face the same problem. 当我生成产品的JSON时,它会生成很好的JSON类别,但是在类别JSON内有另一个指向JSON产品的JSON,因此创建了一个巨大的JSON文件,我尝试通过删除虚拟的来解决此问题,但是每次更新模型我面临同样的问题。 is there any way to solve?
有什么办法解决吗?
If you are using Newtonsoft.Json, then you can simply apply the attribute [JsonIgnore]
to the properties you wish to ignore. 如果使用的是Newtonsoft.Json,则只需将属性
[JsonIgnore]
应用于要忽略的属性。
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
[JsonIgnore]
public virtual ICollection<Product> Products { get; set; }
}
With this setup, whenever category is serialized to Json, it will ignore the Products collection 使用此设置,每当将类别序列化为Json时,它将忽略Products集合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.