【问题标题】:.NET Core 3 Web API: [FromBody] how bind abstract class property.NET Core 3 Web API:[FromBody] 如何绑定抽象类属性
【发布时间】:2021-02-22 11:20:46
【问题描述】:

我有 API 模型请求的类层次结构:

public class Human
{
    public string Name { get; set; }
    public byte Age { get; set; }
    public IEnumerable<HumanData> Data { get;  set; }
}

public abstract class HumanData
{
    public Guid Id { get; set; }
    public string Text { get; set; }
    public virtual string Type => GetType().Name;
}

public class HealthData : HumanData
{
    public byte Weight { get; set; }
    public byte Growth { get; set; }
    public string Pressure { get; set; }
}

public class AddressData : HumanData
{
    public string City { get; set; }
    public string Street { get; set; }
    public uint House { get; set; }
    public uint Apartment { get; set; }
}

我创建了用于添加人类的 API:

[ApiController]
public class HumanController : ControllerBase
{
    ...

    [HttpPost]
    public async Task<IActionResult> Add([FromBody] Human humanRequest)
    {
        //Functionality
    }
}

我调用 API 并传递请求模型:

{
"Name":"Alex",
"Age":30
"Data":[{
    "Id": "10",
    "Text":"Residence address",
    "City":"Elblag",
    "Street":"1 maja",
    "House":"95",
    "Type":"AddressData",
},{
    "Id": "5",
    "Text":"Residence address",
    "Weight":"75",
    "Growth":"179",
    "Pressure":"120/85",
    "Type":"HealthData",
}]}

但是请求模型没有被拉入humanRequest 变量。原因在于Data 字段HumanData 类层次结构。如何进行这样的绑定?

【问题讨论】:

  • 您使用的是哪个 JSON 序列化程序?
  • 我正在使用 NewtonsoftJson。

标签: c# .net asp.net-core asp.net-core-webapi asp.net-core-3.1


【解决方案1】:

如果您像这样更改您的动作签名,您可以获得模型的唯一方法

[HttpPost]
    public async Task<IActionResult> Add([FromBody] JObject humanRequest)
    {

        //Functionality
    }
but you will have untyped Object in this case and you have to read it like this:
var name=humanRequest["Name"].ToString();

如果您的数据结构将更改为这样的方式会更好:

public class Human
{
   public Guid Id { get; set; }
  
    public string Name { get; set; }
    public byte Age { get; set; }
public Guid AddressId {get;set;}
public Guid HealthId {get;set;}
    public virtual AddressData Address { get;  set; }
 public virtual HealthData Health { get;  set; }
}

public class HealthData
{
public Guid Id {get; set;}
  public string Text { get; set; }
    public byte Weight { get; set; }
    public byte Growth { get; set; }
    public string Pressure { get; set; }
}

public class AddressData 
{
public Guid Id {get; set;}
  public string Text { get; set; }
    public string City { get; set; }
    public string Street { get; set; }
    public uint House { get; set; }
    public uint Apartment { get; set; }
}

您的 Json 模型应该如下所示

{
Id: "0f8fad5b-d9cb-469f-a165-70867728950e"
"Name":"Alex",
"Age":30,
AddressId="0f8fad5b-d9cb-469f-a165-70867728950e",
HealthId="0f8fad5b-d9cb-469f-a165-70867728950d",
"Address":{
    "Id": "0f8fad5b-d9cb-469f-a165-70867728950e",
    "Text":"Residence address",
    "City":"Elblag",
    "Street":"1 maja",
    "House":"95",
    "Type":"AddressData"
},
"Health":{
    "Id": "0f8fad5b-d9cb-469f-a165-70867728950d",
    "Text":"Residence address",
    "Weight":"75",
    "Growth":"179",
    "Pressure":"120/85",
    "Type":"HealthData"
}
}

但老实说,我不明白为什么人类需要 3 张桌子。在大多数情况下,您将拥有一对一的关系。我会考虑将所有这 3 个表合并到 1 中。

【讨论】:

    猜你喜欢
    • 2019-01-01
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多