我已經在1和2教程中創建了產品web-API
。第二個教程是調用web-api
的控制檯應用程序。現在,當我創建一個單一的產品,然後它是查看能力。但是,當我創建不止一種產品時,只有最後一個條目才能被查看。爲了更好的理解,請看下面的代碼。使用控制檯應用程序從WEB API獲取所有數據
static HttpClient client = new HttpClient();
static void ShowProduct(Product product)
{
Console.WriteLine($"Name: {product.Name}\tPrice: {product.Price}\tCategory: {product.Category}");
}
static async Task<Uri> CreateProductAsync(Product product)
{
HttpResponseMessage response = await client.PostAsJsonAsync("api/product/", product);
response.EnsureSuccessStatusCode();
// return URI of the created resource.
return response.Headers.Location;
}
static async Task<Product> GetProductAsync(string path)
{
Product product = null;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
product = await response.Content.ReadAsAsync<Product>();
}
return product;
}
static async Task RunAsync()
{
int a;
decimal price = 0;
string name = null, category = null;
char option;
//string url;
client.BaseAddress = new Uri("http://localhost:7361/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Uri url = null;
try
{
label:
Console.Write("1. Create a product\n");
Console.Write("2. View products\n");
Console.Write("3. Update a product\n");
Console.Write("4. Delete a product\n");
Console.Write("5. Exit\n");
Console.WriteLine("\nEnter your choice: ");
a = Convert.ToInt32(Console.ReadLine());
switch(a)
{
case 1:
Console.WriteLine("Enter name of the product: ");
name = Convert.ToString(Console.ReadLine());
Console.WriteLine("\nEnter price of the product: ");
price = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("\nEnter category of the product: ");
category = Convert.ToString(Console.ReadLine());
// Create a new product
Product product = new Product { Name = name, Price = price, Category = category };
url = await CreateProductAsync(product);
Console.WriteLine($"Created at {url}");
Console.WriteLine("Want to create more product(s)? (y/n): ");
option = Convert.ToChar(Console.ReadLine());
if(option == 'y' || option == 'Y')
{
Console.Clear();
goto case 1;
}
else if(option == 'n' || option == 'N')
{
Console.Clear();
goto label;
}
break;
case 2:
// Get the product
product = await GetProductAsync(url.PathAndQuery);
ShowProduct(product);
break;
//case 3:...
//case 4:...
//case 5:...
}
我想查看所有創建的產品。爲此,我必須在創建產品時創建產品列表。但問題是我不知道該在哪裏做?
更新1
在調試代碼的static async Task<Product> GetProductAsync(string path)
的path
得到最後一個產品ID爲 'API /產品/ 2',但我想是這樣的 'API /產品/'。路徑包括最後輸入的id
。
更新2
爲了讓所有的產品我在product = await GetProductAsync("http://localhost:7361/api/product");
進入url
但現在運行的應用程序後,我得到下面的錯誤。
Cannot deserialize the current JSON object (e.g. {「name」:「value」})
更新3
通過檢查Postman
以下API響應是我得到
[
{
"Id": 1,
"Name": "yo-yo",
"Category": "toys",
"Price": 45
},
{
"Id": 2,
"Name": "mobile",
"Category": "electronics",
"Price": 45000
}]
任何幫助將高度讚賞
你從API獲得的JSON響應是什麼? 'GetProductAsync'只返回一個Product。 'ShowProduct'方法只有一個Product實例作爲參數,並且您只將一個產品傳遞給'ShowProduct'參數。所以肯定它只會顯示一個產品。您需要檢查當您向其請求所有產品時API返回的內容。 –
@ChetanRanpariya我在'Update 2'中完成了它。 – faisal1208
您需要知道您從API獲取的JSON,以瞭解您爲什麼獲得'無法反序列化當前的JSON對象錯誤。看起來API會向您返回產品集合,並且您試圖將其反序列化爲單個產品。如果你分享API的JSON響應,我可以提供一些具體的解決方案。 –