api - blazor

main
Peace 11 months ago
parent a72819442e
commit 949d2061d2
  1. 4
      AspNetCoreLearnOfficial.sln
  2. 18
      MyApi/Controllers/WeatherForecastController.cs
  3. 71
      MyBlazorApp/Components/Pages/Weather.razor
  4. 7
      MyBlazorApp/Program.cs
  5. 29
      MyBlazorApp/Services/WeatherForecastService.cs

@ -15,9 +15,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContosoPizza", "WebUIWithRa
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "API-Blazor", "API-Blazor", "{28BC88DF-A712-4EFA-9209-7A95027B1BE0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyApi", "MyApi\MyApi.csproj", "{1F7ECC29-0FDD-48D9-843D-CB467D0EC9A5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyApi", "MyApi\MyApi.csproj", "{1F7ECC29-0FDD-48D9-843D-CB467D0EC9A5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyBlazorApp", "MyBlazorApp\MyBlazorApp.csproj", "{762A7F54-B790-40E2-AB92-C562843E299A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyBlazorApp", "MyBlazorApp\MyBlazorApp.csproj", "{762A7F54-B790-40E2-AB92-C562843E299A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

@ -19,15 +19,19 @@ namespace MyApi.Controllers
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
public async Task<IEnumerable<WeatherForecast>> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
return await Task.Run(async () =>
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
await Task.Delay(3000);
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToArray();
});
}
}
}

@ -1,25 +1,25 @@
@page "/weather"
@attribute [StreamRendering]
@using MyBlazorApp.Services
@inject WeatherForecastService ForecastService
<PageTitle>Weather</PageTitle>
<h3>Weather Forecast ☀</h3>
<h1>Weather</h1>
<p>This component demonstrates showing data.</p>
@if (forecasts == null)
@if (isLoading)
{
<p><em>Loading...</em></p>
<p><em>불러오는 중...</em></p>
}
else
else if (forecasts is not null && forecasts.Any())
{
<div>
<label class="text-info">앞으로 @(forecasts.Count())일에 대한 일기예보</label>
</div>
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
<th>날짜</th>
<th>온도 (C)</th>
<th>온도 (F)</th>
<th>요약</th>
</tr>
</thead>
<tbody>
@ -35,30 +35,39 @@ else
</tbody>
</table>
}
else
{
<p>유효한 데이터가 없습니다.</p>
}
@code {
private WeatherForecast[]? forecasts;
private IEnumerable<WeatherForecast> forecasts;
private bool isLoading = true;
protected override async Task OnInitializedAsync()
{
// Simulate asynchronous loading to demonstrate streaming rendering
await Task.Delay(500);
var startDate = DateOnly.FromDateTime(DateTime.Now);
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
}).ToArray();
await LoadForecasts();
}
private class WeatherForecast
private async Task LoadForecasts()
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public string? Summary { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
try
{
Console.WriteLine("Call Forecasts");
forecasts = await ForecastService.GetForecastsAsync();
Console.WriteLine($"Received {forecasts.Count()} reulsts");
}
catch (Exception ex)
{
Console.WriteLine($"Error fetching data: {ex.Message}");
}
finally
{
isLoading = false;
Console.WriteLine("Call StateHasChanged");
StateHasChanged();
Console.WriteLine("End StateHasChanged");
}
}
}
}

@ -1,4 +1,5 @@
using MyBlazorApp.Components;
using MyBlazorApp.Services;
namespace MyBlazorApp
{
@ -12,6 +13,12 @@ namespace MyBlazorApp
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
// Add HTTPClient (API)
builder.Services.AddHttpClient<WeatherForecastService>("MyApi", client =>
{
client.BaseAddress = new Uri("http://localhost:5122");
});
var app = builder.Build();
// Configure the HTTP request pipeline.

@ -0,0 +1,29 @@
namespace MyBlazorApp.Services
{
public class WeatherForecastService
{
private readonly HttpClient _httpClient;
public WeatherForecastService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<IEnumerable<WeatherForecast>> GetForecastsAsync()
{
var res = await _httpClient.GetFromJsonAsync<IEnumerable<WeatherForecast>>("weatherforecast");
return res;
}
}
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}
Loading…
Cancel
Save