22using FrontEndBackEndDemo . Web . Options ;
33using Microsoft . AspNetCore . Mvc ;
44using Microsoft . Extensions . Options ;
5- using System . Collections . Generic ;
65using System . Diagnostics ;
7- using System . IO ;
8- using System . Net . Http ;
96using System . Text . Json ;
10- using System . Threading . Tasks ;
117
12- namespace FrontEndBackEndDemo . Web . Controllers
8+ namespace FrontEndBackEndDemo . Web . Controllers ;
9+
10+ public class HomeController : Controller
1311{
14- public class HomeController : Controller
15- {
16- private readonly IHttpClientFactory _httpClientFactory ;
17- private readonly IOptionsMonitor < BackEndOptions > _backEndOptionOptionsAccessor ;
12+ private readonly IHttpClientFactory _httpClientFactory ;
13+ private readonly IOptionsMonitor < BackEndOptions > _backEndOptionOptionsAccessor ;
1814
19- public HomeController (
20- IHttpClientFactory httpClientFactory ,
21- IOptionsMonitor < BackEndOptions > backEndOptionOptionsAccessor )
22- {
23- _httpClientFactory = httpClientFactory ;
24- _backEndOptionOptionsAccessor = backEndOptionOptionsAccessor ;
25- }
15+ public HomeController (
16+ IHttpClientFactory httpClientFactory ,
17+ IOptionsMonitor < BackEndOptions > backEndOptionOptionsAccessor )
18+ {
19+ _httpClientFactory = httpClientFactory ;
20+ _backEndOptionOptionsAccessor = backEndOptionOptionsAccessor ;
21+ }
2622
27- public async Task < IActionResult > Index ( )
23+ public async Task < IActionResult > Index ( )
24+ {
25+ HttpClient httpClient = _httpClientFactory . CreateClient ( ) ;
26+ HttpResponseMessage backEndApiResponseMessage = await httpClient . GetAsync (
27+ _backEndOptionOptionsAccessor . CurrentValue . BackEndApiEndPoint ) ;
28+ if ( backEndApiResponseMessage . IsSuccessStatusCode )
2829 {
29- HttpClient httpClient = _httpClientFactory . CreateClient ( ) ;
30- HttpResponseMessage backEndApiResponseMessage = await httpClient . GetAsync (
31- _backEndOptionOptionsAccessor . CurrentValue . BackEndApiEndPoint ) ;
32- if ( backEndApiResponseMessage . IsSuccessStatusCode )
33- {
34- using Stream backEndApiResponseStream =
35- await backEndApiResponseMessage . Content . ReadAsStreamAsync ( ) ;
36- IEnumerable < WeatherForecast > ? weatherForecast =
37- await JsonSerializer . DeserializeAsync < IEnumerable < WeatherForecast > > ( backEndApiResponseStream ) ;
38- return View ( weatherForecast ) ;
39- }
40- return NotFound ( ) ;
30+ using Stream backEndApiResponseStream =
31+ await backEndApiResponseMessage . Content . ReadAsStreamAsync ( ) ;
32+ IEnumerable < WeatherForecast > ? weatherForecast =
33+ await JsonSerializer . DeserializeAsync < IEnumerable < WeatherForecast > > ( backEndApiResponseStream ) ;
34+ return View ( weatherForecast ) ;
4135 }
36+ return NotFound ( ) ;
37+ }
4238
43- public IActionResult Privacy ( )
44- {
45- return View ( ) ;
46- }
39+ public IActionResult Privacy ( )
40+ {
41+ return View ( ) ;
42+ }
4743
48- [ ResponseCache ( Duration = 0 , Location = ResponseCacheLocation . None , NoStore = true ) ]
49- public IActionResult Error ( )
50- {
51- return View ( new ErrorViewModel { RequestId = Activity . Current ? . Id ?? HttpContext . TraceIdentifier } ) ;
52- }
44+ [ ResponseCache ( Duration = 0 , Location = ResponseCacheLocation . None , NoStore = true ) ]
45+ public IActionResult Error ( )
46+ {
47+ return View ( new ErrorViewModel { RequestId = Activity . Current ? . Id ?? HttpContext . TraceIdentifier } ) ;
5348 }
54- }
49+ }
0 commit comments