Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class NamedClientModel : PageModel
{
private readonly IHttpClientFactory _clientFactory;
public IEnumerable<Employee> Employees { get; private set; }
public bool GetEmployeesError { get; private set; }
public bool HasEmployees => Employees.Any();
public NamedClientModel(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public async Task OnGet()
{
var request = new HttpRequestMessage(HttpMethod.Get,
"employees/getall");
var client = _clientFactory.CreateClient("example");
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
using var responseStream = await response.Content.ReadAsStreamAsync();
Employees = await JsonSerializer.DeserializeAsync
<IEnumerable<Employee>>(responseStream);
}
else
{
GetEmployeesError = true;
Employee = Array.Empty<Employees>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment