问题描述: 当使用 Blazor HttpClient 3.2.0 的 Get 方法调用 API 时,可能会抛出异常,异常的原因是响应头的 content-type 与 GetFromJsonAsync 方法不兼容。
解决方法: 要解决这个问题,可以使用以下两种方法之一:
Response.ContentType = "application/json";
var response = await httpClient.GetStreamAsync(apiUrl);
var data = await JsonSerializer.DeserializeAsync(response, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
其中,T 是要反序列化的对象类型。
这两种方法都可以解决 Blazor HttpClient 3.2.0 Get 方法调用抛出异常的问题,选择其中适合你的场景的方法进行处理。