Web API kullanarak çalışmak istediğim özel bir karmaşık türüm var.
public class Widget
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
Ve işte benim web API denetleyici yöntemim. Bu nesneyi şu şekilde göndermek istiyorum:
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;
}
}
Ve şimdi System.Net.HttpClientyönteme çağrı yapmak için kullanmak istiyorum . Bununla birlikte, PostAsyncyönteme ne tür bir nesnenin aktarılacağından ve nasıl oluşturulacağından emin değilim . İşte bazı örnek müşteri kodları.
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();
});
HttpContentNesneyi, web API'sinin anlayacağı şekilde nasıl oluşturabilirim ?