2016-09-01 39 views
1

請參閱我的示例web api action,它採用一個字符串類型參數。WebAPI:使用查詢字符串的PostAsync不起作用

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

} 

,並呼籲像HTTP客戶端下面這樣的方式,但沒有工作,給錯誤未找到

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 
    }    

} 

有人請參閱我的代碼,並告訴我在哪裏,我在調用代碼所犯的錯誤?感謝

+0

要更具體地說明錯誤發生的位置,以及什麼是「未找到」_ – Takarii

+0

response.IsSuccessStatusCode不會返回成功,並且web api操作不會被調用。 –

回答

0

[RoutePrefix( 「API /客戶」)] 公共類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; 
    } 

}

您可以加入的方法參數[FromBody]關鍵字?

+0

如果我的通話不正確,還會添加通話代碼。請先看看我的通話代碼,並告訴我這是對的? –

+0

只有[FromBody]發生錯誤? –

+0

我不確定你爲什麼打電話PostAsync?你可以試試client.Post() –