Skip to content

Instantly share code, notes, and snippets.

@emrekizildas
Created December 7, 2019 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emrekizildas/035db2ad9875dd8bb287cb6766af42e6 to your computer and use it in GitHub Desktop.
Save emrekizildas/035db2ad9875dd8bb287cb6766af42e6 to your computer and use it in GitHub Desktop.
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