Search code examples
c#blazorblazor-webassemblyblazor-client-side

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


I have a problem -

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


Solution

  • Use

    System.Net.Http.Json.HttpClientJsonExtensions.PostAsJsonAsync
    

    Instead of

    System.Net.Http.HttpClientExtensions.PostAsJsonAsync
    

    You may need some of these using:

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

    Remove any reference to System.Net.Http.HttpClientExtensions Below is code to install System.Net.Http.Json, if not installed

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