HttpClient not supporting PostAsJsonAsync method C#

前端 未结 12 967
花落未央
花落未央 2020-11-27 10:37

I am trying to call a web API from my web application. I am using .Net 4.5 and while writing the code I am getting the error HttpClient does not contain a defin

相关标签:
12条回答
  • 2020-11-27 11:25

    PostAsJsonAsync is no longer in the System.Net.Http.dll (.NET 4.5.2). You can add a reference to System.Net.Http.Formatting.dll, but this actually belongs to an older version. I ran into problems with this on our TeamCity build server, these two wouldn't cooperate together.

    Alternatively, you can replace PostAsJsonAsyncwith a PostAsync call, which is just part of new dll. Replace

    var response = client.PostAsJsonAsync("api/AgentCollection", user).Result;
    

    With:

    var response = client.PostAsync("api/AgentCollection", new StringContent(
       new JavaScriptSerializer().Serialize(user), Encoding.UTF8, "application/json")).Result;
    

    Note that JavaScriptSerializer is in the namespace: System.Web.Script.Serialization.

    You will have to add an assembly reference in your csproj: System.Web.Extensions.dll

    See https://code.msdn.microsoft.com/windowsapps/How-to-use-HttpClient-to-b9289836

    0 讨论(0)
  • 2020-11-27 11:26

    Yes, you need to add a reference to

    System.Net.Http.Formatting.dll
    

    This can be found in the extensions assemblies area.

    A good way of achieving this is by adding the NuGet package Microsoft.AspNet.WebApi.Client to your project.

    0 讨论(0)
  • 2020-11-27 11:30

    As already debatted, this method isn't available anymore since .NET 4.5.2. To expand on Jeroen K's answer you can make an extension method:

    public static async Task<HttpResponseMessage> PostAsJsonAsync<TModel>(this HttpClient client, string requestUrl, TModel model)
    {
        var serializer = new JavaScriptSerializer();
        var json = serializer.Serialize(model);
        var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
        return await client.PostAsync(requestUrl, stringContent);
    }
    

    Now you are able to call client.PostAsJsonAsync("api/AgentCollection", user).

    0 讨论(0)
  • 2020-11-27 11:31

    I had this issue too on a project I'd just checked out from source control.

    The symptom was the error described above and a yellow warning triangle on a reference to System.Net.Http.Formatting

    To fix this, I removed the broken reference and then used NuGet to install the latest version of Microsoft.AspNet.WebApi.Client.

    0 讨论(0)
  • 2020-11-27 11:31

    Just expanding Jeroen's answer with the tips in comments:

    var content = new StringContent(
        JsonConvert.SerializeObject(user), 
        Encoding.UTF8, 
        MediaTypeNames.Application.Json);
    
    var response = await client.PostAsync("api/AgentCollection", content);
    
    0 讨论(0)
  • 2020-11-27 11:32

    For me I found the solution after a lot of try which is replacing

    HttpClient
    

    with

    System.Net.Http.HttpClient
    
    0 讨论(0)
提交回复
热议问题