2012-05-07 11 views
6

auf dem Controller ist Put senden, wie folgend:Httpclient PutAsync keinen Parameter zu api

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

Das Problem ist auf die, wenn sie über folgende

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

diese API raubend ‚Dieser Code doesn t übergeben Sie den Parameternamen an den Controller. Ich habe sogar versucht changeing uri zu/putname /“+ Name

Antwort

16

Hier ist, was für mich funktioniert.

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

und meine Aktionsmethode:

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

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

und das Modell

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

-Stan

+0

Danke für die Antwort. Ich denke, was du gesagt hast, sollte funktionieren. Folgendes habe ich getan. var cUri = neuer Uri ("http: // localhost/cart/coupon"); var jsonString = JsonConvert.SerializeObject (neu {id = "abc"}); HttpResponseMessage cartResponse; mit (HttpContent httpContent = new StringContent (jsonString)) { httpContent.Headers.ContentType = neue MediaTypeHeaderValue ("application/json"); cartResponse = httpClient.PostAsync (cUri, httpContent) .Result; } – fcmaine

+0

Ich benutze das gleiche wie Sie vorgeschlagen, aber nicht funktioniert. –

+0

Schön, das hat auch bei mir funktioniert –

0

Für mich hat es richtig funktioniert:

  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 
      } 
Verwandte Themen