无法将 JSON 值转换为 HttpDelete 响应中的类列表

问题描述 投票:0回答:1

使用.Net Core我开发了一个HttpDelete控制器

[HttpDelete("dqc")]
[AllowAnonymous]
public async Task<IActionResult> DeleteDQCAsync([FromBody] IEnumerable<DqcDto> dqcs, CancellationToken cancellationToken)
{
    try
    {
        var response = await _managerService.DeleteDqcAsync(dqcs, cancellationToken);
        if (response)
        {
            return Ok($"DQC codes have been deleted successfully");
        }
        else
        {
            return BadRequest("Delete selected DQC codes failed.");
        }
    }
    catch (Exception)
    {
        throw;
    }
}

DqcDto类设计为:

public class DqcDto
{
    public string? DqcCode { get; set; }
    public string? DqcDescription { get; set; }
    public string? Project { get; set; }
    public string? ProjectGroup { get; set; }
    public string? Task { get; set; }
    public bool? IsCheck { get; set; }
}

当我在 Body 中使用带有负载的 Postman 进行测试时

{
    "dqcs": [
        {
            "DqcCode":"DQC-005-B-1",
            "DqcDescription":"Is the naming",
            "Project":"D1",
            "ProjectGroup":"D",
            "Task":"T1",
            "IsCheck":true
        },
        {
            "DqcCode":"DQC-109-1-1",
            "DqcDescription":"Checked-In",
            "Project":"D2",
            "ProjectGroup":"D",
            "Task":"T2",
            "IsCheck":true
        }
    ]
}

我收到以下错误

"errors": {
        "$": [
            "The JSON value could not be converted to System.Collections.Generic.IEnumerable`1[IQC_Database_Management_API.Dtos.DqcDto]. Path: $ | LineNumber: 0 | BytePositionInLine: 1."
        ],
        "dqcs": [
            "The dqcs field is required."
        ]
    }

我已经检查过,没有拼写问题。我不明白为什么它显示需要提交“dqcs”。我已经把它分配在身体里了。 有人可以告诉我哪里出错了吗?

asp.net-core-webapi http-delete
1个回答
0
投票

你的控制器调用一个数组,所以你的主体有效负载应该是

[
    {
        "DqcCode":"DQC-005-B-1",
        "DqcDescription":"Is the naming",
        "Project":"D1",
        "ProjectGroup":"D",
        "Task":"T1",
        "IsCheck":true
    },
    {
        "DqcCode":"DQC-109-1-1",
        "DqcDescription":"Checked-In",
        "Project":"D2",
        "ProjectGroup":"D",
        "Task":"T2",
        "IsCheck":true
    }
]

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.