-
Notifications
You must be signed in to change notification settings - Fork 1
/
Index.cshtml.cs
48 lines (41 loc) · 1.16 KB
/
Index.cshtml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace WebApp.Pages.WeatherForecast
{
public class IndexModel : PageModel
{
private const string ApiGetUrl = "WeatherForecast";
private readonly ILogger<IndexModel> _logger;
private readonly WeatherForecastApiClient client;
public IndexModel(
ILogger<IndexModel> logger,
WeatherForecastApiClient client)
{
_logger = logger;
this.client = client;
}
public string GetApiUrl => client.GetApiUri(ApiGetUrl);
public List<WeatherForecast> Items { get; set; }
public string ErrorMessage { get; private set; }
public async Task OnGet()
{
try
{
var response = await client.GetAsync(ApiGetUrl);
var content = await response.Content.ReadAsStringAsync();
Items = JsonConvert.DeserializeObject<List<WeatherForecast>>(content);
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
}
}
}
}