2016-09-01 9 views
1

1つの文字列型パラメータを取るサンプルWeb APIのアクションを参照してください。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

レスポンスです.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()を試してみてください。 –

関連する問題