0

How can I POST a complex type to a Web Api 2 controller action, from a C# client? I see a lot of information about it but for older versions of the framework. What's the correct way to do it in .NET 4.5.1?

At the moment I have:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://localhost:60892/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    //client.PostAsJsonAsync() doesn't seem to exist or I am missing something
    //client.PostAsync() can't find a way to post a complex type    

    if (response.IsSuccessStatusCode)
    {

    }
}
Dante
  • 3,833
  • 4
  • 38
  • 55

1 Answers1

2

You will find the PostAsJsonAsync extension method in the Micorosft ASP.NET Web API 2.1 Client NuGet package.

Nevertheless, if you set the HttpContent appropriately the PostAsync method should work. An example can be found in this discussion.

Community
  • 1
  • 1
Horizon_Net
  • 5,959
  • 4
  • 31
  • 34