【发布时间】:2022-01-25 23:10:44
【问题描述】:
我有一个 API 项目,我需要使用该 API 开发一个 Web 项目,我编写了一些代码但无法找到异常和问题,并且无法从链接中获取数据。
这是我的服务代码:
public async Task<IEnumerable<AgentReadDto>> GetAgent()
{
IEnumerable<AgentReadDto> agents = new List<AgentReadDto>();
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://localhost:44331/api/");
var response = client.GetAsync("Agent/GetAllAgent");
response.Wait();
var result = response.Result;
if (result.IsSuccessStatusCode)
{
var readTask =JsonConvert.DeserializeObject<IList<AgentReadDto>>(await result.Content.ReadAsStringAsync());
agents = readTask;
}
}
return agents;
}
我的控制器代码如下所示:
public IActionResult AgentLists()
{
var agentsList = _agentRespositoryWeb.GetAgent();
if (agentsList != null )
{
ViewBag.Message = "There was a problem retrieving agent from the database or no agents exists";
}
ViewBag.SuccessMessage = TempData["SuccessMessage"];
return View(agentsList);
}
我的 api 返回以下值:
{
"agentDetail": [
{
"usersId": 85,
"firstName": "Amit",
"lastName": "One",
"gender": "Male",
"informationTips": [
{
"video": "https://www.w3schools.com/html/movie.mp4"
},
{
"video": "https://www.w3schools.com/html/movie.mp4"
},
]
},
{
"usersId": 86,
"firstName": "Amit",
"lastName": "Two",
"gender": "Male",
"informationTips": [
{
"video": "https://www.w3schools.com/html/movie.mp4"
}
]
}
]
}
【问题讨论】:
-
您好,错误消息说您的剃刀视图需要
IEnumerable<UserReadDto>类型。你能分享一下你的UserReadDto和AgentReadDto的模型设计吗?另外,GetAllAgent的代码是什么?response.Result的值是否正确?
标签: c# asp.net-core asp.net-core-webapi