0

CRUDの操作を持つWEB APIがあります。テストのために私はConsole applicationを作成しました。作成し、すべての詳細が正常に動作しています。今私はidフィールドを使用して製品を取得したい。以下は私のコードidが私にエラーcannot convert string to system.net.http.httpcompletionoptionを与えているidフィールドを使用してすべての製品を入手する

static HttpClient client = new HttpClient(); 
static void ShowProduct(Product product) 
    { 

     Console.WriteLine($"Name: {product.Name}\tPrice: {product.Price}\tCategory: {product.Category}", "\n"); 
    } 
static async Task<Product> GetProductAsyncById(string path, string id) 
    { 
     Product product = null; 
     HttpResponseMessage response = await client.GetAsync(path,id); 
     if (response.IsSuccessStatusCode) 
     { 
      product = await response.Content.ReadAsAsync<Product>(); 
     } 
     return product; 
    } 
case 3: 

        Console.WriteLine("Please enter the Product ID: "); 
        id = Convert.ToString(Console.ReadLine()); 

        // Get the product by id 
        var pr = await GetProductAsyncById("api/product/", id); 
        ShowProduct(pr); 

        break; 

client.GetAsync(path,id)

です。このため私はそれに関連するすべての記事をチェックしました。しかし、まだ正しい解決策を見つけることができません。

すべてのヘルプは非常にstringように、第2引数を受け入れ何の方法GetAsync()がないため、このエラーが出る

+0

私が発見したカップルhttps://stackoverflow.com/questions/14520762/system-net-http-httpcontent-does-not-contain-a-definition-for-readasasync-an)を参照してください。試してみてください。 –

答えて

2

をいただければ幸いです。あなたのAPIのURLは次のようなものであれば

また、GET要求をしているあなたは、すなわち、URLにidを渡す必要があります:http://domain:port/api/Products、その後、あなたの要求のURLがidはあなたが取得したい製品のidがあるhttp://domain:port/api/Products/idする必要があります。 C#6以上

HttpResponseMessage response = await client.GetAsync(path + "/" +id); 

または場合:あなたがでGetAsync()に呼び出す

変更は[こちら]ソリューション(の

HttpResponseMessage response = await client.GetAsync(path + $"/{id}"); 
+0

それは私が行方不明だった。 – faisal1208

関連する問題