I created ASP.NET Core MVC .NET 8 application. I added simple WebApi ProductController which returns Json list of products.
Then I added Blazor WebAssembly Standalone app to the solution and included one page from the Blazor app to my ASP.NET Core app. I want to implement rich UI for products with this Blazor component. To get data (products) I call the ProductController with RestSharp component. Then strange happens – the product list refreshed for a second and then disappear. If I try test data (simple mock with list of products) all works fine. I checked the ProductController WebApi it works fine, returns json data.
Please help to fix this.
Blazor component file:
@inject NavigationManager NavManager
<h3>Products</h3>
@if(_viewModel == null)
{
<p><em>Loading...</em></p>
}
else
{
<ul>
@foreach (var product in _viewModel.Products)
{
<li>@product.Name</li>
}
</ul>
@if(_viewModel.Products.Count == 0)
{
<p><em>No products found</em></p>
}
}
@code {
private ProductIndexViewModel _viewModel;
private IDataProviderFactory _dataProviderFactory;
private IProductDataProvider _productDataProvider;
protected override async Task OnInitializedAsync()
{
_dataProviderFactory = GetDataProviderFactory();
_productDataProvider = _dataProviderFactory.CreateProductDataProvider();
_viewModel = await _productDataProvider.GetByGroupAndPageAsync(0, 1, 5);
}
private IDataProviderFactory GetDataProviderFactory()
{
return new RestDataProviderFactory(NavManager.BaseUri);
//return new TestDataProviderFactory();
}
}
RestSharp call:
public async Task<ProductIndexViewModel> GetByGroupAndPageAsync(int productGroupId, int pageNumber, int itemsPerPage)
{
string uri = $"{GetApiUri()}/GetByGroupAndPage/{productGroupId}/{pageNumber}/{itemsPerPage}";
var request = new RestRequest(uri, Method.Get);
var restResponse = await restClient.ExecuteGetAsync<ProductIndexViewModel>(request);
if (restResponse.IsSuccessful)
{
ProductIndexViewModel viewModel = restResponse.Data;
return viewModel;
}
return new ProductIndexViewModel();
}