POST 请求 JSON 数组

POST Request of a JSON Array

我能够为 post 将 1 个对象写入控制器中的数据库编写一个控制器。但是我现在想 post 1 API 调用中的多个对象使用 JSON

我的实体

public class CustomerInvoiceLine: BaseEntity
    {
        public SchoolFee SchoolFee { get; set; }
        public int SchoolFeeId { get; set; }
        public string Description { get; set; }
        public int Quantity { get; set; }
        public int Amount { get; set; }
        public int CustomerInvoiceId { get; set; }
    }

我有一个 DTO 如下:

public class CustomerInvoiceLineAddDto
    {
        public int SchoolFeeId { get; set; }
        public string Description { get; set; }
        public int Quantity { get; set; }
        public int Amount { get; set; }
        public int CustomerInvoiceId { get; set; }
    }

我已经创建了一个存储库:

public async Task AddCustomerInvoiceLineAsync(CustomerInvoiceLine customerInvoiceLine)
        {
            _context.CustomerInvoiceLines.Add(customerInvoiceLine);
            await _context.SaveChangesAsync();
        }

最后是控制器

[HttpPost]
        public async Task<ActionResult> AddCustomerInvoiceLine(CustomerInvoiceLineAddDto invoiceLineAddDto)
        {
            CustomerInvoiceLine invoiceLineDetails = new CustomerInvoiceLine();
            _mapper.Map(invoiceLineAddDto, invoiceLineDetails);
            await _unitOfWork.CustomerInvoiceLineRepository.AddCustomerInvoiceLineAsync(invoiceLineDetails);
            return Ok();
        }

上面的控制器在 post 请求 JSON 中的 1 项时工作正常。

然后我尝试更改它以接收一个 JSON 数组。

public async Task<ActionResult> AddCustomerInvoiceLine(CustomerInvoiceLineAddDto invoiceLineAddDto)
        {
            string json = "";
            CustomerInvoiceLine invoiceLineDetails = new CustomerInvoiceLine();
            CustomerInvoiceLineAddDto invoiceLines = JsonConvert.DeserializeObject<CustomerInvoiceLineAddDto>( json );
            _mapper.Map(invoiceLineAddDto, invoiceLines);
            await _unitOfWork.CustomerInvoiceLineRepository.AddCustomerInvoiceLineAsync(invoiceLineDetails);
            return Ok();
        }

我的JSON请求:

[
    {
        "schoolFeeId": 1,
        "customerInvoiceId": 18,
        "description": "School Fees for 2022",
        "quantity": 1,
        "amount": 5000
    },
    {
        "schoolFeeId": 1,
        "customerInvoiceId": 18,
        "description": "School Fees for 2021",
        "quantity": 1,
        "amount": 3000
    }
]

有人可以帮助理解如何反序列化 JSON 正文然后处理到数据库吗?

如果要接受数组,请确保控制器接受数组。然后 automapper 足够聪明,可以将一种类型的数组转换为另一种类型的数组。

public async Task<ActionResult> AddCustomerInvoiceLine(CustomerInvoiceLineAddDto[] invoiceLinesAddDto)
{
    var invoiceLineDetails = _mapper.Map<List<CustomerInvoiceLine>>(invoiceLinesAddDto);
    await _unitOfWork.CustomerInvoiceLineRepository.AddCustomerInvoiceLinesAsync(invoiceLineDetails);
    return Ok();
}

在您的工作单元中,添加一个函数来处理列表的添加

public async Task AddCustomerInvoiceLinesAsync(IEnumerable<CustomerInvoiceLine> customerInvoiceLines)
{
    _context.CustomerInvoiceLines.AddRange(customerInvoiceLines);
    await _context.SaveChangesAsync();
}

请注意,您不能让一个控制器方法同时处理数组请求和单个项目请求。我建议定义两个不同的端点,以便 api 的消费者知道哪个需要数组,哪个不需要。