GetFromJsonAsync
当我们使用命名的 HttpClients 时,我们没有可用的方法有什么原因吗?当我切换到命名 HttpClients 时,我必须安装 NewtonSoftJson 来反序列化响应。
注入 Razor 组件
@inject HttpClient httpClient
@code{
public async Task GetProfileAsync()
{
User user = await httpClient.GetFromJsonAsync<User>("user/getprofile/10");
}
}
注入服务/类
public ProfileViewModel(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task GetProfileAsync()
{
var requestMessage = new HttpRequestMessage(HttpMethod.Get, "user/getprofile/10");
var response = await _httpClient.SendAsync(requestMessage);
var responseBody = await response.Content.ReadAsStringAsync();
User user = JsonConvert.DeserializeObject<User>(responseBody);
}
谢谢法赫德穆拉吉