@page "/fetchdata"
@inject HttpClient Http
Weather forecast
Weather forecast
This component demonstrates fetching data from the server.
@if (forecasts == null)
{
Loading...
}
else
{
Date |
Temp. (C) |
Temp. (F) |
Summary |
@foreach (var forecast in forecasts)
{
@forecast.Date.ToShortDateString() |
@forecast.TemperatureC |
@forecast.TemperatureF |
@forecast.Summary |
}
}
@code {
private WeatherForecast[]? forecasts;
protected override async Task OnInitializedAsync()
{
forecasts = await Http.GetFromJsonAsync("sample-data/weather.json");
}
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public string? Summary { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
}