2017-06-02 8 views
0

コントローラメソッドからポスト変数を読み取れるようにしたいと思います。WebApi - 私の投稿変数はいつもnullですか?

私は現在、以下のコードを持っている:私はテストするために、次のコードを使用してい

[HttpPost] 
public IHttpActionResult BuildPartitions([FromBody]string PartitionBuildDate) 
{ 
} 

:オンライン探し

using (HttpClient httpClient = new HttpClient()) 
{ 
    var values = new Dictionary<string, string> 
    { 
     { "PartitionBuildDate", "24-May-2017" } 
    }; 
    var content = new FormUrlEncodedContent(values); 
    var response = httpClient.PostAsync("http://localhost:55974/api/Controller/BuildPartitions", content); 
    var responseString = response.Result.Content; 
} 

を、これはでポスト変数を送信および受信の両方のために正しく見えますC#を使用しますが、PartitionBuildDate変数は常にnullです。

答えて

1

content-typeヘッダーを追加してみてください。私は、JSON変換用のNewtonsoft JSON.NETを使用しています

public class PostParameters 
{ 
    public string PartitionBuildDate {get;set;} 
} 

[HttpPost] 
public IHttpActionResult BuildPartitions([FromBody]PostParameters parameters) 
{ 
    //you can access parameters.PartitionBuildDate 
} 

string postBody = JsonConvert.SerializeObject(yourDictionary); 

var response = client.PostAsync(url, new StringContent(postBody, Encoding.UTF8, "application/json")); 

var responseString = response.Result.Content; 

また、あなたのWeb API側では、クラス内であなたのPOSTパラメータをラップしてみてください

関連する問題