Asp.net web api 如何调用异步任务函数?
我的AllWebApiOperations类代码是Asp.net web api 如何调用异步任务函数?,asp.net-web-api,async-await,Asp.net Web Api,Async Await,我的AllWebApiOperations类代码是 public AllWebApiOperations(string apiURI) { client = new HttpClient(); client.BaseAddress = new Uri(apiURI); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accep
public AllWebApiOperations(string apiURI)
{
client = new HttpClient();
client.BaseAddress = new Uri(apiURI);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<string> GetDataAsync(string route)
{
string result= string.Empty;
HttpResponseMessage response = await client.GetAsync(route);
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsAsync<string>();
}
return result;
}
我的代码web api工作正常,如下所示
但调用函数时出错,如下所示
我不确定为什么会出现此错误,尝试过谷歌搜索,但无法找到解决方案。找到了答案:
public async Task<string> GetDataAsync(string route)
{
string result= string.Empty;
HttpResponseMessage response = await client.GetAsync(route);
if (response.IsSuccessStatusCode)
{
**result = await response.Content.ReadAsAsync<string>();**
}
return result;
}
public async Task<string> GetDataAsync(string route)
{
string result= string.Empty;
HttpResponseMessage response = await client.GetAsync(route);
if (response.IsSuccessStatusCode)
{
**result = await response.Content.ReadAsAsync<string>();**
}
return result;
}
**result = await response.Content.ReadAsStringAsync();**