Уровень пользовательского интерфейса вызывает API через универсальный репозиторий API с использованием HttpClient.
Структура моего проекта примерно следующая:
Код: Выделить всё
Core
└─ DataAccess
└─ Repository
└─ ApiRepositoryBase.cs
Kutuphane.API
└─ Controllers
└─ DilController.cs
Kutuphane.Model
└─ Entity
└─ Dil.cs
Kutuphane.UI
└─ Services
Однако я получаю следующее исключение:
Код: Выделить всё
System.Text.Json.JsonException: The JSON value could not be converted to
System.Collections.Generic.List[Kutuphane.Model.Entity.Dil].
Path: $ | LineNumber: 0 | BytePositionInLine: 1.
Код: Выделить всё
var data = await response.Content.ReadFromJsonAsync();
Код: Выделить всё
public async Task GetListAsync(string endpoint, Expression? filter = null)
{
try
{
using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(_baseUrl.Trim());
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", _token);
var response = await httpClient.GetAsync($"api/{endpoint}");
if (!response.IsSuccessStatusCode)
return new ErrorDataResult(response.ReasonPhrase ?? "Api error");
var data = await response.Content.ReadFromJsonAsync();
if (data == null)
return new ErrorDataResult("Deserialization failed");
if (filter != null)
{
var compiledFilter = filter.Compile();
data = data.Where(compiledFilter).ToList();
}
return new SuccessDataResult(data);
}
catch (Exception ex)
{
return new ErrorDataResult(ex.Message);
}
}
Код: Выделить всё
public class Dil
{
public int Id { get; set; }
public string Ad { get; set; }
}
Что может привести к тому, что ReadFromJsonAsync() выдаст это исключение?
Подробнее здесь: https://stackoverflow.com/questions/799 ... -listt-whe
Мобильная версия