Riot API deserialization not saving in razorpages C#

58 views Asked by At

I am trying to use RIOT API to read Summoners info, but I got stuck right after the deserialization. When I am trying to save the JSON properties into an object, it just doesn't save any of the properties.

model:

public class LOLSummoner
{
    [JsonProperty("id")]
    public string id { get; set; }
    [JsonProperty("accountId")]
    public string accountId { get; set; }
    [JsonProperty("puuid")]
    public string puuid { get; set; }
    [JsonProperty("name")]
    public string name { get; set; }
    [JsonProperty("profileIconId")]
    public string profileIconId { get; set; }
    [JsonProperty("revisionDate")]
    public string revisionDate { get; set; }
    [JsonProperty("summonerLevel")]
    public string summonerLevel { get; set; }
}

readapi method:

public async void OnPostCallAPI()
{
    string Baseurl = "https://eun1.api.riotgames.com/lol/summoner/v4/summoners/by-name/PolskaHrozba123";

    try
    {
        using (var client = new HttpClient())
        {
            HttpRequestMessage request = new HttpRequestMessage();
            request.RequestUri = new Uri(Baseurl);
            request.Method = HttpMethod.Get;
            request.Headers.Add("X-Riot-Token", "RGAPI-020de469-e1b9-4bdf-aea1-175739868a0b");
            HttpResponseMessage response = await client.SendAsync(request);         
            var responseString = await response.Content.ReadAsStringAsync();
            var statusCode = response.StatusCode;
            
            if (response.IsSuccessStatusCode)
            {
                var responses = JsonConvert.DeserializeObject<LOLSummoner>(responseString);
                
                LOLSummoner TestovaciUzivatel = new()
                {
                    id = responses.id,
                    accountId = responses.accountId,
                    puuid = responses.puuid,
                    name = responses.name,
                    profileIconId = responses.profileIconId,
                    revisionDate = responses.revisionDate,
                    summonerLevel = responses.summonerLevel
                };
                
                summonername = TestovaciUzivatel.name.ToString();             
            }
            else
            {              
            }
        }
    }
    catch (Exception ex)
    {

        throw;
    }
}

If you see any mistakes let me know! Gotta find a solution.

1

There are 1 answers

0
Drapys On BEST ANSWER

The problem was in async, after removing async from the method, it somehow actually saved the info in the object. No clue why it didnt with async.

Thank you @Drag and Drop for helping! :)