0
I’m developing a site that requests an api and shows the data to users, but I’m having problems:
Use of unassigned local variable 'value'
Use of unassigned local variable 'date'
To better understand:
Page code with error:
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public Global Global { get; private set; }
public Country[] Countries { get; private set; }
public DateTime Date { get; private set; }
public IMemoryCache MemoryCache { get; private set; }
public IndexModel(ILogger<IndexModel> logger, IMemoryCache memoryCache)
{
_logger = logger;
MemoryCache = memoryCache;
}
public async Task OnGetAsync()
{
try
{
string key_countries = "countries_cache"; // Chave do cache.
string key_global = "global_cache";
string key_date = "date_cache";
if (!MemoryCache.TryGetValue<Country[]>(key_countries, out Country[] values) &&
(!MemoryCache.TryGetValue<Global>(key_global, out Global value) &&
(!MemoryCache.TryGetValue<DateTime>(key_date, out DateTime date)))) // Verifica se há dados em cache.
{
var create = RestService.For<IGetRoot>("https://api.covid19api.com/summary");
var result = await create.GetAsync(); // sou var result = create.GetAsync().GetAwaiter().GetResult();
_logger.LogInformation(result.ToString());
Global = result.Global;
Countries = result.Countries;
Date = result.Date;
MemoryCache.Set(key_countries, Countries, System.TimeSpan.FromMinutes(30)); // Salva os dados em cache e define um tempo de expiração
MemoryCache.Set(key_global, Global, System.TimeSpan.FromMinutes(30));
MemoryCache.Set(key_date, Date, System.TimeSpan.FromMinutes(30));
}
else
{
Global = value;
Countries = values;
Date = date;
}
}
catch (Exception e)
{
_logger.LogInformation("Erro na requisição http: " + e.Message);
}
}
}
you have assigned a value that does not exist! no
else
of your codevalue
anddate
is not stating, if you are confused because these values should have their ownif
andelse
!– novic