Sul controller Mettere è la seguente:HttpClient PutAsync non invia un parametro di api
[HttpPut]
[ActionName("putname")]
public JsonResult putname(string name)
{
var response = ...
return Json(response);
}
La questione è sul quando si consumano questa API tramite seguente
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;
}
Questo codice doesn' t passare il nome del parametro al controller. Ho anche provato uri changeing a/putname /" + nome
Grazie per la risposta. Penso che quello che hai detto dovrebbe funzionare. Di seguito è quello che ho fatto. var cUri = new Uri ("http: // localhost/cart/coupon"); var jsonString = JsonConvert.SerializeObject (new {id = "abc"}); HttpResponseMessage cartResponse; utilizzando (HttpContent httpContent = new StringContent (jsonString)) { httpContent.Headers.ContentType = new MediaTypeHeaderValue ("application/json"); cartResponse = httpClient.PostAsync (cUri, httpContent) .Result; } – fcmaine
Sto usando la stessa cosa che hai proposto come risposta ma non funziona. –
Bello, questo ha funzionato anche per me –