2012-05-07 10 views
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; 
    } 

निम्नलिखित के माध्यम से इस एपीआई लेने वाली इस कोड नहीं करता है पर है ' नियंत्रक को पैरामीटर नाम पास नहीं करते हैं। मैं भी/putname/"+ नाम के uri changeing की कोशिश की

उत्तर

16

यहाँ क्या मेरे लिए काम करता है:।

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

और मेरे कार्रवाई विधि:

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

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

और मॉडल

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

-स्टान

+0

उत्तर के लिए धन्यवाद। मुझे लगता है कि आपने जो कहा वह काम करना चाहिए। मैंने जो किया वह निम्नलिखित है। var cUri = नया उरी ("http: // localhost/cart/coupon"); var jsonString = JsonConvert.SerializeObject (नया {id = "abc"}); HttpResponseMessage cartResponse; का उपयोग कर (एचटीपीकंटेंट httpContent = नया स्ट्रिंगकंटेंट (जेसनस्ट्रिंग)) { httpContent.Headers.ContentType = नया MediaTypeHeaderValue ("application/json"); कार्ट रेस्पॉन्स = httpClient.PostAsync (curi, httpContent) .समान; } – fcmaine

+0

मैं वही चीज़ का उपयोग कर रहा हूं जैसा आपने उत्तर के रूप में प्रस्तावित किया लेकिन काम नहीं कर रहा था। –

+0

अच्छा, यह मेरे लिए भी काम किया –

0

मेरे लिए यह सही ढंग से काम करता है:

  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 
      } 
संबंधित मुद्दे