認証トークンを使用してHttpClientを使用してWebAPIに投稿しようとしています。HttpClientを使用してWebApiに投稿するには?
しかし私は実際に送信している値ではなく、WebAPIメソッドで常にデフォルト値を取得しています。
これは私のコードです:
C#コンソールAPP:
public static async Task<string> Rent(HttpClient client, string token, int idCommunityAmenity, int idHome, DateTime startDate, DateTime endDate)
{
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:50634/api/amenity/RentCommunityAmenity");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
var postContent = new
{
idCommunityAmenity = idCommunityAmenity,
idHome = idHome,
startDate = startDate,
endDate = endDate
};
request.Content = new StringContent(JsonConvert.SerializeObject(postContent), Encoding.UTF8, "application/json");
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
私はそれを点検するときので
[HttpPost("RentCommunityAmenity")]
public async Task<JsonResult> Post([FromBody]int idCommunityAmenity, [FromBody]int idHome, [FromBody]DateTime startDate, [FromBody]DateTime endDate)
{
var communityAmenity = new AmenityReservation
{
IdCommunityAmenity = idCommunityAmenity,
StartDate = startDate,
EndDate = endDate,
IdHome = idHome
};
_context.AmenityReservation.Add(communityAmenity);
await _context.SaveChangesAsync();
return new JsonResult(true);
}
WebAPIのは、私の推測では、コンテンツが正しく設定されていないことです私はjsonの文字列が表示されません。私は、POSTメソッドを打つとき
は私が取得:助けをidCommunityAmenity = 0、idHome = 0、...
感謝。