通过 Postman 放置方法Web Api?发送参数更新时出现问题

vfhzx4xs  于 2022-11-29  发布在  Postman
关注(0)|答案(1)|浏览(234)

我在.net core 6.0 web api上工作。
Postman 测试web api
我的问题如何发送参数到web api更新
方法中。
如何发送id和body上的对象以更新动作。

{
    "itemId": 4,
    "itemNameAR": "قلم",
    "itemNameEN": "pen2",
    "costTypeId": 1,
    "minLimitQunatity": 1,
    "costAccountID": 1,
    "departmentID": 1,
    "itemCategoryId": 1,
    "description": "1",
    "hasExpireDate": true,
    "isActive": true,
    "createdBy": 1,
    "modifiedBy": 1,
    "createdDate": "0001-01-01T00:00:00",
    "modifiedDate": "0001-01-01T00:00:00",
    "itemattribute": null
}

发送API时如下

https://localhost:7235/api/items?id=4

那么请问如何解决问题?

[HttpPut("{id}")]
          public async Task<IActionResult> Update(int id, UpdateItemCommand command)
          {
              if (id != command.Id)
              {
                  return BadRequest();
              }
              return Ok(await Mediator.Send(command));
          }

我尝试发送什么api request Web API没有捕获或命中,虽然我运行我的Web API。

    • 更新后**我改变网址后的人如下
https://localhost:7235/api/4

但仍有问题没有解决
错误显示400错误请求详细信息错误

{
    "errors": {
        "Fax": [
            "The Fax field is required."
        ],
        "City": [
            "The City field is required."
        ],
        "Phone": [
            "The Phone field is required."
        ],
        "Region": [
            "The Region field is required."
        ],
        "Address": [
            "The Address field is required."
        ],
        "Country": [
            "The Country field is required."
        ],
        "PostalCode": [
            "The PostalCode field is required."
        ],
        "ContactName": [
            "The ContactName field is required."
        ],
        "ContactTitle": [
            "The ContactTitle field is required."
        ],
        "CustomerName": [
            "The CustomerName field is required."
        ]
    },
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-efde167cb6a7fa5af42abff9d055e2db-92ff55a318cb7a29-00"
}
qlfbtfca

qlfbtfca1#

将请求URL更改为,

https://localhost:7235/api/items/4

因为您的控制器已配置为,

[HttpPut("{id}")]

其指示路径参数而不是查询参数。

相关问题