Created
December 7, 2019 10:30
-
-
Save emrekizildas/035db2ad9875dd8bb287cb6766af42e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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