6
在把控制器如下:HttpClient的PutAsync不參數發送到API
[HttpPut]
[ActionName("putname")]
public JsonResult putname(string name)
{
var response = ...
return Json(response);
}
的問題是在通過以下
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;
}
消耗這個API時,該代碼沒有按」將參數名稱傳遞給控制器。我甚至嘗試量變到質變的URI/putname /」 +名
感謝您的答覆。我認爲你所說的應該起作用。以下是我所做的。 var cUri = new Uri(「http:// localhost/cart/coupon」); var jsonString = JsonConvert.SerializeObject(new {id =「abc」}); HttpResponseMessage cartResponse; (HttpContent httpContent = new StringContent(jsonString)) { httpContent.Headers.ContentType = new MediaTypeHeaderValue(「application/json」); cartResponse = httpClient.PostAsync(cUri,httpContent).Result; } – fcmaine
我用同樣的東西,你建議作爲答案,但不工作。 –
不錯,這也適用於我 –