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)
    {

    }
}

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So, yeah, I was missing the 2.1 Client nuget package. Thanks for the reply!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.