Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用RESTAPI的.NET Core 2.1不';t返回结果_C#_Asp.net_Asp.net Web Api_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# 使用RESTAPI的.NET Core 2.1不';t返回结果

C# 使用RESTAPI的.NET Core 2.1不';t返回结果,c#,asp.net,asp.net-web-api,asp.net-core,asp.net-core-mvc,C#,Asp.net,Asp.net Web Api,Asp.net Core,Asp.net Core Mvc,这就是我目前尝试使用RESTAPI的方式 private HttpClient client = new HttpClient(); public HomeController(IAuthenticationClient authenticationClient) { client.DefaultRequestHeaders.Accept.Clear(); client.BaseAddress = new Uri("http://localhost:55260/api/Accou

这就是我目前尝试使用RESTAPI的方式

private HttpClient client = new HttpClient();
public HomeController(IAuthenticationClient authenticationClient)
{
    client.DefaultRequestHeaders.Accept.Clear();
    client.BaseAddress = new Uri("http://localhost:55260/api/Accounts/");
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    this.authenticationClient = authenticationClient;
}

public IActionResult Index()
{
    var result = GetList();
    return View();
}

public async Task<List<string>> GetList()
{
    HttpResponseMessage response = await client.GetAsync("GetList");
    if (response.IsSuccessStatusCode)
    {
        return await response.Content.ReadAsAsync<List<string>>();
    }
    return new List<string>();
}
private-HttpClient=new-HttpClient();
公共HomeController(IAAuthenticationClient authenticationClient)
{
client.DefaultRequestHeaders.Accept.Clear();
client.BaseAddress=新Uri(“http://localhost:55260/api/Accounts/");
client.DefaultRequestHeaders.Accept.Add(新的System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(“应用程序/json”);
this.authenticationClient=authenticationClient;
}
公共IActionResult索引()
{
var result=GetList();
返回视图();
}
公共异步任务GetList()
{
HttpResponseMessage response=wait client.GetAsync(“GetList”);
if(响应。IsSuccessStatusCode)
{
return wait response.Content.ReadAsAsync();
}
返回新列表();
}
这是正在调用的API:

[HttpGet]
[Route("GetList")]
[AllowAnonymous]
public ActionResult<IEnumerable<string>> GetList()
{
    return new string[] { "value1", "value2" };
}
[HttpGet]
[路线(“获取列表”)]
[异名]
公共操作结果GetList()
{
返回新字符串[]{“value1”,“value2”};
}
问题是,如果我在Postman上测试它,我会得到正确的结果,即value1和value2,然而,当我从api客户端调用它时,我会得到这个奇怪的结果,而不是正确的结果


我已经试过这么多不同的指南,但似乎没有一个有效。是否有一个库来处理.NETCore2.1的RESTAPI?还是我最好只使用httpclient?

正如您原始问题的答案中所述,当您调用
GetList
方法时,您缺少关键字
wait
。让我们回顾一下您的代码:

public IActionResult Index()
{
    var result = GetList();
    return View();
}

public async Task<List<string>> GetList()
{
    HttpResponseMessage response = await client.GetAsync("GetList");
    if (response.IsSuccessStatusCode)
    {
        return await response.Content.ReadAsAsync<List<string>>();
    }
    return new List<string>();
}
我们已将方法签名更改为包含
async
关键字,并将结果包装到
任务中。
此处的更改意味着您现在可以
等待
GetList
方法返回的任务完成,
result
变量将保存
List
数据,然后您可以将这些数据传递给视图


有关async和await的更多信息,请参见原始问题的答案中所述。调用
GetList
方法时,您缺少关键字
await
。让我们回顾一下您的代码:

public IActionResult Index()
{
    var result = GetList();
    return View();
}

public async Task<List<string>> GetList()
{
    HttpResponseMessage response = await client.GetAsync("GetList");
    if (response.IsSuccessStatusCode)
    {
        return await response.Content.ReadAsAsync<List<string>>();
    }
    return new List<string>();
}
我们已将方法签名更改为包含
async
关键字,并将结果包装到
任务中。
此处的更改意味着您现在可以
等待
GetList
方法返回的任务完成,
result
变量将保存
List
数据,然后您可以将这些数据传递给视图


更多信息可在async and Wait上找到

如果它是从邮递员处获得的,请单击“保存”按钮附近的代码链接从邮递员处获得代码如果它是从邮递员处获得的,请单击“保存”按钮附近的代码链接从邮递员处获得代码