JSON 无法读取数据

JSON Data Could Not Read

我有一个 Post 方法作为 GetProductList,我 post 一些输入,该方法过滤了数据和 returns 一个列表。我用 JSON 在 Postman 上尝试了这个方法。我 post 输入,但它们出现 null。我添加了调试 ss。为什么会这样?

方法:

        [HttpPost]
        [Route("GetProductList")]
        public async Task<IActionResult> GetProductList(int id, string? productName, string? companyName, int productInfo, int operationType, string? startDate, string? endDate)
        {
            var result = _productsService.GetProductList(id,productName, companyName, productInfo, operationType, startDate, endDate);

        return Ok(result);
    }

JSON:

{"id":152,"productName":null,"companyName":"XCompany","productInfo":5,"startDate":"2021-08-03","endDate":"2021-08-03","operationType":1}

调试的屏幕截图

创建视图模型总是一个好主意,尤其是当您使用 post

public class ProductViewModel
{

public int Id {get; set;}
public string ProductName {get; set;}
public string CompanyName {get; set;}
.....

}

当你使用 Postman 时,尝试添加 [FromBody]

总是好的
[HttpPost]
 [Route("GetProductList")]
 public async Task<IActionResult> GetProductList([FromBody] ProductViewModel viewModel)
 {
 var result = _productsService.GetProductList(viewModel.Id,viewModel.ProductName, ....);

        return Ok(result);
    }

你可以在测试后删除 [FromBody],如果它在网络中不起作用