下面是一个使用Blazor和HttpClient以及Config.json的代码示例:
@using System.Net.Http
@using Microsoft.Extensions.Configuration
@inject HttpClient Http
@inject IConfiguration Config
@code {
private async Task GetWeatherForecast()
{
var apiUrl = Config.GetValue("ApiUrl");
var response = await Http.GetFromJsonAsync(apiUrl);
// 处理响应数据
}
}
{
"ApiUrl": "https://api.example.com/weatherforecast"
}
请注意,上述示例假设已定义WeatherForecast类来表示天气预报数据。