2016-06-18 4 views
0

Web APIメソッドにlist<restaurant >を追加し、レストランとrest_locationの間で異なるタイプの混合を返すプロジェクトで作業しています。 レストランとrest_locationの関係は1対多です。Web APIがASP.NET Web APIのリストを取得して別の型を返す方法

私はそれを実行すると、エラーが表示されます。

ServerのWeb API:

{ "メッセージ" "要求されたリソースは、HTTPメソッドをサポートしていません 'GET'。"}:

[HttpPost] 
[Route("api/Restaurants/res_by_locat/{x}/{res_loc}")] 
[ResponseType(typeof(Restaurant))] 
[ResponseType(typeof(Rest_Location))] 
public HttpResponseMessage GetResturantsBylocation([FromUri]int x,[FromBody] List<Rest_Location> res_loc) 
{ 
    List<Table> table = new List<Table>(); 
    var lstitem = from t1 in res_loc 
        from t2 in db.Restaurants.Where(y => y.R_ID == t1.R_ID) 
            .DefaultIfEmpty() 
        select new { t1.L_Adress, t2.R_Name }; 

    //foreach (var item in lstitem) 
    //{ 
    // table.Add(item); 
    //} 
    if (lstitem == null || !lstitem.Any()) 
    { 
     throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); 
    } 

    else return ControllerContext.Request.CreateResponse(HttpStatusCode.OK, new { lstitem });   
} 

クライアント:

protected void btn_ddl_Click(object sender, EventArgs e) 
{ 
    if (locationddl.SelectedValue == "0") 
    { 
     lbl_result.Text = "Chose a Location First ! ";    
    } 
    else 
    { 
     try 
     { 
      HttpClient client = new HttpClient(); 
      client.BaseAddress = new Uri("http://localhost:10566/"); 
      client.DefaultRequestHeaders.Accept.Clear(); 
      client.DefaultRequestHeaders.Accept.Add(
       new MediaTypeWithQualityHeaderValue("application/json")); 
      var fromddl = locationddl.SelectedItem.Text; 
      //lbl_result.Text = fromddl; 
      var response = client.GetAsync("api/Locations/getid_by_name/" + fromddl).Result; 
      // if (Page.IsValid) 
      { 
       if (response.IsSuccessStatusCode) 
       { 
        Location ll = response.Content.ReadAsAsync<Location>().Result; 
        int x = ll.L_ID; 

        lbl_msg.Text = x.ToString(); 

        var response2_get_ids_Rests = client.GetAsync("api/Rest_Location/res_by_locat/" + x).Result; 

        if (response2_get_ids_Rests.IsSuccessStatusCode) 
        { 
         List<Rest_Location> rests_locations = response2_get_ids_Rests.Content.ReadAsAsync<List<Rest_Location>>().Result; 
         //var items= rests_locations.FirstOrDefault(rl => rl.L_ID == x); 
         // GridView2.DataSource = rests_locations; 
         // GridView2.DataBind(); 
         HttpResponseMessage response3_get_resturants_by = client.PostAsJsonAsync("api/Restaurants/res_by_locat/" + x , rests_locations).Result; 
         if (response3_get_resturants_by.IsSuccessStatusCode) 
         { 
          var news= response3_get_resturants_by.Content.ReadAsAsync<List<Restaurant>>().Result; 
          GridView1.DataSource = news; 
          GridView1.DataBind(); 

          lbl_msg.Text = "Search succesed"; 
         } 
         else 
         { 
          lbl_test.Text = " not found "; 
         } 
        } 
        else 
        { 
         lbl_test.Text = " not found "; 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      lbl_msg.Text = "Couldn't Found Resaurants ! " + ex.ToString(); 
     } 
    } 
} 

答えて

2

エンドポイントは、それがだけあなたのクライアントはGETを使用してエンドポイントにアクセスしようとしているPOST

を使用してアクセスすることができることを意味している、この属性、[HttpPost]を持っています。

GetAsyncの代わりにclient.PostAsyncメソッドを使用する必要があります。

+0

クライアントを実行してもデータが見つかりませんでしたが動作しません –

関連する問題