代码之家  ›  专栏  ›  技术社区  ›  ahmed barbary

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

  •  0
  • ahmed barbary  · 技术社区  · 3 年前

    我的工作 .net core 6.0 web api。我在上面临问题

    测试 web api 邮差。

    我的问题是如何将参数发送到webapi更新

    方法

    所以如何发送主体上带有对象的id来更新操作。

    {
        "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,但没有被抓住或击中。

    Api not hit

    更新的帖子 我在post-man上更改url如下

    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"
    }
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   Charles Han    3 年前

    将您的请求URL更改为,

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

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

    [HttpPut("{id}")]
    

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