我有一個非常簡單的WebAPI類,它返回JSON爲我的獲取方法。我郵政方法是:爲什麼我從Web API控制器上的Post方法獲取空值?
// POST: api/Diff
public void Post([FromBody]string value)
{
System.Diagnostics.Debug.WriteLine(value);
}
和我打電話從控制檯應用程序兩種方法:
using (var client = new HttpClient())
{
// New code:
client.BaseAddress = new Uri("http://localhost:50541/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// New code:
HttpResponseMessage response = await client.GetAsync("api/diff/1");
if (response.IsSuccessStatusCode)
{
var diff = await response.Content.ReadAsAsync<Diff>();
Console.WriteLine("{0}\t${1}", diff.Text, diff.Revision);
}
var blah = new Diff() { Text = "Gizmo", Revision = 999};
response = await client.PostAsJsonAsync("api/diff", blah);
的獲取通話作品。我在調試器中驗證了在調用PostAsJson方法之前等於不爲空。但是,在API的post方法的調試器中,值爲空。