Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9250103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:22:04+00:00 2026-06-18T10:22:04+00:00

I have a custom complex type that I want to work with using Web

  • 0

I have a custom complex type that I want to work with using Web API.

public class Widget
{
    public int ID { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

And here is my web API controller method. I want to post this object like so:

public class TestController : ApiController
{
    // POST /api/test
    public HttpResponseMessage<Widget> Post(Widget widget)
    {
        widget.ID = 1; // hardcoded for now. TODO: Save to db and return newly created ID

        var response = new HttpResponseMessage<Widget>(widget, HttpStatusCode.Created);
        response.Headers.Location = new Uri(Request.RequestUri, "/api/test/" + widget.ID.ToString());
        return response;
    }
}

And now I’d like to use System.Net.HttpClient to make the call to the method. However, I’m unsure of what type of object to pass into the PostAsync method, and how to construct it. Here is some sample client code.

var client = new HttpClient();
HttpContent content = new StringContent("???"); // how do I construct the Widget to post?
client.PostAsync("http://localhost:44268/api/test", content).ContinueWith(
    (postTask) =>
    {
        postTask.Result.EnsureSuccessStatusCode();
    });

How do I create the HttpContent object in a way that web API will understand it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:22:05+00:00Added an answer on June 18, 2026 at 10:22 am

    The generic HttpRequestMessage<T> has been removed. This :

    new HttpRequestMessage<Widget>(widget)
    

    will no longer work.

    Instead, from this post, the ASP.NET team has included some new calls to support this functionality:

    HttpClient.PostAsJsonAsync<T>(T value) sends “application/json”
    HttpClient.PostAsXmlAsync<T>(T value) sends “application/xml”
    

    So, the new code (from dunston) becomes:

    Widget widget = new Widget()
    widget.Name = "test"
    widget.Price = 1;
    
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("http://localhost:44268");
    client.PostAsJsonAsync("api/test", widget)
        .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode() );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I`m trying to display classes that have properties of type some custom class inside
Lets say I have a custom data type that looks something like this: public
I have custom classes that I currently instantiate within App.xaml as resources. I want
I have been attempting recently to write a web service that returns a custom
Let's suppose I have this object: [Serializable] public class MyClass { public int Age
So I have two custom complex types like this (oversimplified for this example): public
I have to build a more complex custom view in Android. The final layout
I have a complex UIView with lots of custom subviews, and I need to
I have custom validation rule: public function customRule($check) { } Inside this rule I
We have custom headers in the Silverlight DataGrid using the ContentTemplate. We've got a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.