2016-09-01 28 views
1

siehe meine Beispiel-Web-API-Aktion, die einen String-Typ-Parameter nehmen.WebAPI: PostAsync mit Abfragezeichenfolge funktioniert nicht

[RoutePrefix("api/customer")] 
public class CustomerController : ApiController 
{ 

     [HttpPost, Route("DeleteCustomer")] 
     public HttpResponseMessage DeleteProduct(string customerID) 
     { 
      HttpResponseMessage response = null; 
      Customer customer = repository.Get(customerID); 
      if (customer == null) 
      { 
       var message = string.Format("No customer found by the ID {0}", customerID); 
       HttpError err = new HttpError(message); 
       response = Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, err); 
       response.ReasonPhrase = message; 
      } 
      else 
      { 
       if(repository.Remove(customerID)) 
       { 
        response = Request.CreateResponse<Customer>(HttpStatusCode.Created, customer); 
        response.ReasonPhrase = "Customer successfully deleted"; 
       } 
       else 
       { 
        var message = string.Format("Due to some error customer not removed"); 
        HttpError err = new HttpError(message); 
        response = Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, err); 
        response.ReasonPhrase = message; 
       } 
      } 


      return response; 
     } 

} 

und mit http-Client wie diese unten Art und Weise fordern aber nicht funktioniert und Nicht

private void btnDelete_Click(object sender, EventArgs e) 
{ 
    var uri = new Uri(ConfigurationManager.AppSettings["baseAddress"] + "/api/customer/DeleteCustomer"); 

    var content = new FormUrlEncodedContent(new[] 
    { 
     new KeyValuePair<string, string>("customerID", "CUS01") 
    }); 

    try 
    { 
     using (var client = new HttpClient()) 
     { 
      using (var response = client.PostAsync(uri, content).Result) 
      { 
       if (response.IsSuccessStatusCode) 
       { 
        MessageBox.Show(response.ReasonPhrase); 
       } 
       else 
       { 
        Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); 
        var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content.ReadAsStringAsync().Result); 
        MessageBox.Show(dict["Message"]); 
       } 
      } 
     } 
    } 
    catch (HttpRequestException ex) 
    { 
     // catch any exception here 
    }    

} 

jemand bitte meinen Code sehen und mich gefunden geben Fehler sagen, wo ich Code in dem Aufruf der Fehler gemacht ? Dank

+0

Genauer gesagt, wo der Fehler auftritt, und was ist _ "nicht gefunden" _ – Takarii

+0

response.IsSuccessStatusCode ist nicht zurück Erfolg und Web-API-Aktion wird nicht aufgerufen. –

Antwort

0

[RoutePrefix ("api/Kunde")] public class CustomerController: ApiController {

[HttpPost, Route("DeleteCustomer")] 
    public HttpResponseMessage DeleteProduct([FromBody]string customerID) 
    { 
     HttpResponseMessage response = null; 
     Customer customer = repository.Get(customerID); 
     if (customer == null) 
     { 
      var message = string.Format("No customer found by the ID {0}", customerID); 
      HttpError err = new HttpError(message); 
      response = Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, err); 
      response.ReasonPhrase = message; 
     } 
     else 
     { 
      if(repository.Remove(customerID)) 
      { 
       response = Request.CreateResponse<Customer>(HttpStatusCode.Created, customer); 
       response.ReasonPhrase = "Customer successfully deleted"; 
      } 
      else 
      { 
       var message = string.Format("Due to some error customer not removed"); 
       HttpError err = new HttpError(message); 
       response = Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, err); 
       response.ReasonPhrase = message; 
      } 
     } 


     return response; 
    } 

}

Könnten Sie hinzufügen [FromBody] Stichwort in Methodenparameter?

+0

auch den Anrufcode hinzufügen, wenn mein Anruf nicht richtig ist. Bitte sehen Sie zuerst meinen Telefoncode und sagen Sie mir, dass das richtig ist? –

+0

Fehler tritt nur für [FromBody] auf? –

+0

Ich bin mir nicht sicher Warum rufen Sie PostAsync? Könnten Sie versuchen, client.Post() –

Verwandte Themen