HttpClient PutAsync不向api发送参数

fcm*_*ine 8 c# json.net asp.net-mvc-3

在控制器上Put如下:

[HttpPut]
[ActionName("putname")]
public JsonResult putname(string name)
{
    var response = ...
    return Json(response);  
}
Run Code Online (Sandbox Code Playgroud)

问题在于通过以下方式消费此API

using (httpClient = new HttpClient())
{
    string name = "abc";
    string jsonString = JsonConvert.SerializeObject(name);
    var requestUrl = new Uri("http:...../controller/putname/");
    using (HttpContent httpContent = new StringContent(jsonString))
    {
        httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        HttpResponseMessage response = httpClient.PutAsync(requestUrl, httpContent).Result;
    }
Run Code Online (Sandbox Code Playgroud)

此代码不会将参数名称传递给控制器​​.我甚至尝试将uri更改为/ putname /"+ name.

Sta*_*nko 26

这对我有用:

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);
Run Code Online (Sandbox Code Playgroud)

和我的行动方法:

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

   if (ModelState.IsValid)
   {
     // ..
   }      
}
Run Code Online (Sandbox Code Playgroud)

和模型

public class AppRating
{
    public int AppId { get; set; }
    public int PlatformId { get; set; }
    public decimal Rating { get; set; }
} 
Run Code Online (Sandbox Code Playgroud)

-Stan