I’m new with c# and i start learning with blazor. I’m trying to get all products from woocommerce api from my store. To check code is working i want to count products in a list but still no success. All the time list with products is empty. This is a full code from razor component:
@page "/woocommercepage"
@using WooCommerceNET.WooCommerce.v3;
@using WooCommerceNET.WooCommerce.v3.Extension;
<p>Products count:@products.Count()</p>
<br />
@code
{
public static List<Product> products = new List<Product>();
public class woo
{
public static async Task<List<Product>> call()
{
RestAPI rest = new RestAPI("https://mypage.com/wp-json/wc/v3/", "ck_000000", "cs_000000");
WCObject wc = new WCObject(rest);
string SKU = "box";
Dictionary<string, string> pDic = new Dictionary<string, string>();
pDic.Add("sku", SKU);
var products = await wc.Product.GetAll(pDic);
return products;
}
}
}
2
Answers
You are creating a class in your razor view that does not get called.
In the
@code
block there are several overrides available. In this case you will need theOnInitializedAsync()
method. This method gets called when your razor view is being served. Make your call here and update your viewstate.I have this code (in VB…sory). See if it helps…
It should not be hard to convert to C# (it will be a good practice for you:))
Then you can use it like this: