如何在返回的JSON中不显示外键ID?

如何在返回的JSON中不显示外键ID?,json,asp.net-web-api,asp.net-core-webapi,Json,Asp.net Web Api,Asp.net Core Webapi,我正在使用.NETCore3.0WebAPI项目。设置关系时,我有一个名为ProductTypeID的模型属性,它是保存ProductType表ID的外键。当我查询UnitOfWork并将此结果返回给控制器时,我希望从JSON中删除productTypeID 产品控制器: [HttpGet] public IEnumerable<Product> Index() { var products = _service.GetAll(); return products;

我正在使用.NETCore3.0WebAPI项目。设置关系时,我有一个名为
ProductTypeID
的模型属性,它是保存
ProductType
表ID的外键。当我查询
UnitOfWork
并将此结果返回给控制器时,我希望从JSON中删除
productTypeID

产品控制器:

[HttpGet]
public IEnumerable<Product> Index()
{
    var products = _service.GetAll();

    return products;
}
public override IEnumerable<Product> GetAll()
{
    return _context.products
        .Include(x => x.ProductType)
        .AsEnumerable();
}
在浏览器上返回JSON:

[
  {
    "name": "Pepperoni",
    "price": 12,
    "description": "Test Description",
    "productTypeID": 1,
    "productType": {
      "name": "Pizza",
      "id": 1,
      "createdAt": "20/01/2020 19:28:26",
      "updatedAt": "20/01/2020 19:28:26"
    },
    "id": 1,
    "createdAt": "20/01/2020 19:28:26",
    "updatedAt": "20/01/2020 19:28:26"
  },
  {
    "name": "Margherita",
    "price": 10,
    "description": "This is a margherita pizza",
    "productTypeID": 1,
    "productType": {
      "name": "Pizza",
      "id": 1,
      "createdAt": "20/01/2020 19:28:26",
      "updatedAt": "20/01/2020 19:28:26"
    },
    "id": 2,
    "createdAt": "20/01/2020 19:28:26",
    "updatedAt": "20/01/2020 19:28:26"
  }
]
我相信如果我创建了一个
ViewModel
,并且只返回我需要的属性,这将起作用。但我想知道是否有一种方法可以做到这一点,而无需创建
ViewModel

您可以添加属性以从JSON中删除
productTypeID

public class Product:BaseModel
{
    [Required]
    public string Name { get; set; }
    [Required]
    [Column(TypeName = "decimal(18,2)")]
    public decimal Price { get; set; }
    [Required]
    public string Description { get; set; }

    [JsonIgnore]
    public int ProductTypeID { get; set; }

    public virtual ProductType ProductType { get; set; }
}

我提出了为每个数据库实体创建视图模型的想法。这似乎是最好的办法。然后我使用AutoMapper将属性映射到viewmodel。感谢所有发表评论的人。

为什么不创建视图模型?这正是视图模型存在的原因。应避免将数据库模型用作数据传输对象。这可能是非常有害的,取决于你做什么。在验证输入时,最好创建单独的类型并根据需要在类型之间正确映射。我会再试一次,并告诉你到底发生了什么,因为这是一个大约一周前,我尝试了这个。感谢您展示Startup.cs?并确保不要将
System.Text.Json
Newtonsoft.Json
混用。
[
  {
    "name": "Pepperoni",
    "price": 12,
    "description": "Test Description",
    "productTypeID": 1,
    "productType": {
      "name": "Pizza",
      "id": 1,
      "createdAt": "20/01/2020 19:28:26",
      "updatedAt": "20/01/2020 19:28:26"
    },
    "id": 1,
    "createdAt": "20/01/2020 19:28:26",
    "updatedAt": "20/01/2020 19:28:26"
  },
  {
    "name": "Margherita",
    "price": 10,
    "description": "This is a margherita pizza",
    "productTypeID": 1,
    "productType": {
      "name": "Pizza",
      "id": 1,
      "createdAt": "20/01/2020 19:28:26",
      "updatedAt": "20/01/2020 19:28:26"
    },
    "id": 2,
    "createdAt": "20/01/2020 19:28:26",
    "updatedAt": "20/01/2020 19:28:26"
  }
]
public class Product:BaseModel
{
    [Required]
    public string Name { get; set; }
    [Required]
    [Column(TypeName = "decimal(18,2)")]
    public decimal Price { get; set; }
    [Required]
    public string Description { get; set; }

    [JsonIgnore]
    public int ProductTypeID { get; set; }

    public virtual ProductType ProductType { get; set; }
}