Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Threading.Tasks;
- namespace EcommerceApp2.Models
- {
- public class ProductService
- {
- HttpClient _client = new HttpClient();
- public ProductsModel GetProducts()
- {
- var resp = _client.GetAsync("http://localhost:52127/EcommerceApi/products/").Result;
- if ( resp.IsSuccessStatusCode)
- {
- List<ProductsModel> products = JsonConvert.DeserializeObject<List<ProductsModel>>(resp.Content.ReadAsStringAsync().Result);
- // return JsonSerializer.Create(products);
- }
- return null;
- }
- public ProductsModel GetProductById(int id)
- {
- var resp = _client.GetAsync("http://localhost:52127/EcommerceApi/products/" + id).Result;
- if ( resp.IsSuccessStatusCode )
- {
- return JsonConvert.DeserializeObject<ProductsModel>(resp.Content.ReadAsStringAsync().Result);
- }
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement