如何从 asp.net 核心 mvc 中的 HttpClient class 调用类型 [HttpPatch] 的 API 操作

How to invoke the API action of type [HttpPatch] from HttpClient class in asp.net core mvc

我的 API 有一个我需要调用的 [HttpPatch] 操作。

[HttpPatch("{id}")]
public StatusCodeResult Patch(int id, [FromBody]JsonPatchDocument<Reservation> patch)
{
    Reservation res = Get(id);
    if (res != null)
    {
        patch.ApplyTo(res);
        return Ok();
    }
    return NotFound();
}

我正在从 HttpClient class 尝试它,但它 没有 .PatchAsync() 方法?

此外,参数的类型为 JsonPatchDocument<Reservation> 那么在调用此操作时如何从客户端发送它?

请帮忙

您必须手动创建一个 HttpRequestMessage 并通过 SendAsync 发送:

var request = new HttpRequestMessage
{
    RequestUri = new Uri("http://foo.com/api/foo"),
    Method = new HttpMethod("patch"),
    Content = new StringContent(json, Encoding.UTF8, "application/json-patch+json")
};
var response = await _client.SendAsync(request);