12

次のように構築された Web API の Delete メソッドを呼び出す必要があるシナリオがあります。

// DELETE: api/products/{id}/headers
[HttpDelete("{id}/headers")]
public void DeleteProductHeaders(int id, [FromBody] string query)
{
}

秘訣は、クエリを取得するには本文を介して送信する必要があり、DeleteAsync には post のように json のパラメーターがないことです。c#でSystem.Net.Httpクライアントを使用してこれを行う方法を知っている人はいますか?

// Delete a product's headers
public void DeleteProductHeaders(int id, string query)
{
    using (var client = GetClient())
    {
        HttpResponseMessage response;
        try
        {
            // HTTP DELETE
            response = client.DeleteAsync($"api/products/{id}/headers").Result;
        }
        catch (Exception ex)
        {
            throw new Exception("Unable to connect to the server", ex);
        }
    }
    return retVal;
}
4

3 に答える 3