2012-05-07 3 views
6

En el controlador de venta es de la siguiente manera:HttpClient PutAsync no envía un parámetro de API

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

La cuestión está en la hora de consumir esta API a través siguiente

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

Este código doesn' t pasar el nombre del parámetro al controlador. Incluso he intentado changeing uri a/putname /" + nombre

Respuesta

16

Esto es lo que funciona para mí:.

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

y mi método de acción:

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

    if (ModelState.IsValid) 
    { 
    // .. 
    }  
} 

y el modelo

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

-Stan

+0

Gracias por la respuesta. Creo que lo que dijiste debería funcionar. Lo siguiente es lo que hice. var cUri = new Uri ("http: // localhost/cart/cupón"); 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; } – fcmaine

+0

Estoy usando lo mismo que propuso como respuesta, pero no funciona. –

+0

Agradable, Esto también funcionó para mí –

0

Para mí funcionó correctamente:

  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 
      } 
Cuestiones relacionadas