如何从asp.net core mvc中的HttpClient类调用[HttpPatch]类型的API动作

问题描述 投票:1回答:1

我的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类尝试它但它没有.PatchAsync()方法?

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

请帮忙

asp.net asp.net-mvc api asp.net-core http-patch
1个回答
2
投票

你必须手动创建一个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);
© www.soinside.com 2019 - 2024. All rights reserved.