HttpClient PutAsync doesn't send a parameter to api

前端 未结 2 634
感情败类
感情败类 2021-01-04 13:08

On the controller Put is as following:

[HttpPut]
[ActionName(\"putname\")]
public JsonResult putname(string name)
{
    var response = ...
    return Json(re         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-04 13:15

    Here is what works for me:

    var jsonString = "{\"appid\":1,\"platformid\":1,\"rating\":3}";
    var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");            
    var message = await _client.PutAsync(MakeUri("App/Rate"), httpContent);
    Assert.AreEqual(HttpStatusCode.NoContent, message.StatusCode);
    

    and my action method:

    public void PutRate(AppRating model)
    {
       if (model == null)
          throw new HttpResponseException(HttpStatusCode.BadRequest);
    
       if (ModelState.IsValid)
       {
         // ..
       }      
    }
    

    and the model

    public class AppRating
    {
        public int AppId { get; set; }
        public int PlatformId { get; set; }
        public decimal Rating { get; set; }
    } 
    

    -Stan

提交回复
热议问题