【问题标题】:HttpClient PutAsync doesn't send a parameter to apiHttpClient PutAsync 不向 api 发送参数
【发布时间】:2016-01-10 05:16:35
【问题描述】:

在控制器上Put如下:

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

问题在于通过以下方式使用此 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;
    }

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

【问题讨论】:

    标签: c# asp.net-mvc-3 json.net


    【解决方案1】:

    这对我有用:

    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);
    

    和我的行动方法:

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

    和模型

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

    -斯坦

    【讨论】:

    • 感谢您的回复。我觉得你说的应该可以。以下是我所做的。 var cUri = new Uri("localhost/cart/coupon"); var jsonString = JsonConvert.SerializeObject(new { id = "abc" }); HttpResponseMessage cartResponse; using (HttpContent httpContent = new StringContent(jsonString)) { httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); cartResponse = httpClient.PostAsync(cUri, httpContent).Result; }
    • 我正在使用与您建议的答案相同的东西,但不起作用。
    【解决方案2】:

    对我来说它工作正常:

                string requestUrl = endpointUri + "/Files/";
                var jsonString = JsonConvert.SerializeObject(new { name = "newFile.txt", type = "File" }); 
    
                HttpContent httpContent = new StringContent(jsonString);
                httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue ("application/json");          
    
                HttpClient hc = new HttpClient();
    
                //add the header with the access token
                hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
    
                //make the put request
                HttpResponseMessage hrm = (await hc.PostAsync(requestUrl, httpContent));
    
                if (hrm.IsSuccessStatusCode)
                {
                   //stuff
                }
    

    【讨论】:

    • 但这适用于 PostAsync。 OP 在询问 PutAsync
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多