Error: CS0121 The call is ambiguous between the following methods or properties

Error: CS0121 The call is ambiguous between the following methods or properties

我有问题-

System.Net.Http.HttpClientExtensions.PostAsJsonAsync(System.Net.Http.HttpClient, string, T, System.Threading.CancellationToken)' and 'System.Net.Http.Json

private async Task Add()
{
    using (var msg = await Http.PostAsJsonAsync<Feedback>("/api/feedbacks", newcust, System.Threading.CancellationToken.None))
    {
        if (msg.IsSuccessStatusCode)
        {
            custs.Add(await msg.Content.ReadFromJsonAsync<Feedback>());
            newcust.title = newcust.rating = newcust.comment = null;
        }
    }

    if (ValidReCAPTCHA)
    {
        var response = await reCAPTCHAComponent.GetResponseAsync();
        try
        {
            ServerVerificatiing = true;
            StateHasChanged();
            await Http.PostAsJsonAsync("/api/sample", new SampleAPIArgs { reCAPTCHAResponse = response });
            Navigation.NavigateTo("/valid");
        }
        catch (HttpRequestException e)
        {
            await JS.InvokeAsync<object>("alert", e.Message);
            ServerVerificatiing = false;
            StateHasChanged();
        }
    }
}

Screenshoot Picture

在 2 个命名空间中找到方法 Http.PostAsJsonAsync - 查看错误消息的实际内容。

您必须对名称进行更多限定,以便编译器知道您要使用哪一个。否则,您必须删除冲突的 using 之一。

就像 PMFKlaus 在他们的评论中所说的那样 - 请 post 代码,而不是屏幕截图。

使用

System.Net.Http.Json.HttpClientJsonExtensions.PostAsJsonAsync

而不是

System.Net.Http.HttpClientExtensions.PostAsJsonAsync

您可能需要其中一些使用:

using System.Net.Http.Json;
using System.Net.Http;
using System.Text.Json;
using System.Text;
using System.Text.Json.Serialization;

删除对 System.Net.Http.HttpClientExtensions 的所有引用 下面是安装代码System.Net.Http.Json,如果没有安装

Install-Package System.Net.Http.Json -Version 5.0.0