// Web App Project
// Index.cshtml.cs
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IHttpClientFactory _httpClientFactory;
public IndexModel(ILogger<IndexModel> logger, IHttpClientFactory httpClientFactory)
{
_logger = logger;
_httpClientFactory = httpClientFactory;
}
public async Task OnGet()
{
await GetAllContacts();
}
private async Task GetAllContacts()
{
var _client = _httpClientFactory.CreateClient();
var response = await _client.GetAsync("https://localhost:44320/api/Contacts");
List<ContactModel> contacts;
if (response.IsSuccessStatusCode)
{
var options = new JsonSerializerOptions
{
// allows javascript and c# variables to match. Variables in javascript are camelCase and C# is PascalCase
PropertyNameCaseInsensitive = true
};
string responseText = await response.Content.ReadAsStringAsync();
contacts = JsonSerializer.Deserialize<List<ContactModel>>(responseText, options);
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}