expose-myit/Client/Pages/FetchData.razor.cs

30 lines
1012 B
C#

using Expose.MyIT.Shared.ViewModels;
using Microsoft.AspNetCore.Components;
using System.Net.Http.Json;
namespace Expose.MyIT.Client.Pages;
public partial class FetchData
{
[Inject] protected HttpClient? HttpClient { get; set; }
[Inject] protected ILogger<FetchData>? Logger { get; set; }
private WeatherForecast[]? _Forecasts;
protected override async Task OnInitializedAsync()
{
if (Logger is null)
throw new NullReferenceException(nameof(Logger));
if (HttpClient is null)
throw new NullReferenceException(nameof(HttpClient));
string controllerName = MyIT.Shared.Models.Stateless.IWeatherForecastController<object>.GetRouteName();
try
{ _Forecasts = await HttpClient.GetFromJsonAsync<WeatherForecast[]>($"api/{controllerName}"); }
catch (Exception)
{
string json = await HttpClient.GetStringAsync($"api/{controllerName}");
Logger.LogInformation(message: json);
}
}
}