Trên bộ điều khiển Đặt là như sau:HttpClient PutAsync không gửi một tham số để api
[HttpPut]
[ActionName("putname")]
public JsonResult putname(string name)
{
var response = ...
return Json(response);
}
Vấn đề là trên khi tiêu thụ API này qua sau
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;
}
Mã này doesn' t chuyển tên tham số cho bộ điều khiển. Tôi thậm chí đã cố gắng changeing uri đến/putname /" + tên
Cảm ơn bạn đã trả lời. Tôi nghĩ những gì bạn nói nên hoạt động. Sau đây là những gì tôi đã làm. var cUri = new Uri ("http: // localhost/cart/coupon"); var jsonString = JsonConvert.SerializeObject (new {id = "abc"}); HttpResponseThanh toán sản phẩmResponse; sử dụng (HttpContent httpContent = new StringContent (jsonString)) { httpContent.Headers.ContentType = new MediaTypeHeaderValue ("application/json"); cartResponse = httpClient.PostAsync (cUri, httpContent) .Result; } – fcmaine
Tôi đang sử dụng điều tương tự như bạn đã đề xuất làm câu trả lời nhưng không hoạt động. –
Đẹp, Điều này cũng có hiệu quả đối với tôi –