【发布时间】:2017-12-25 19:09:30
【问题描述】:
我有一个 API 的 URL。我想在控制器中进行操作以获取所有数据
BranchesController.cs:
public Task<IEnumerable<BranchVm>> GetAllAsync()
{
string baseUrl = "http://api.diamond.smart-gate.net/api/Branches/GetBranches";
var client = new HttpClient();
var task = client.GetStringAsync(baseUrl);
return task.ContinueWith<IEnumerable<BranchVm>>(innerTask =>
{
var json = innerTask.Result;
return JsonConvert.DeserializeObject<BranchVm[]>(json);
});
}
Branch.js:
columns: [
{
"data": 'branchArName',
"name": "branchArName",
"autoWidth": true,
"orderable": true
},
{
"data": 'branchEnName',
"name": "branchEnName",
"autoWidth": true,
"orderable": true,
},
],
ajax: {
url: "/Branches/GetAllAsync",
dataSrc: ''
}
它不返回任何数据,但是当我调试它时,我在 innerTaslk.Result 中有所有数据,但 var json 等于 null。所以,我不知道为什么?
【问题讨论】:
-
这对我来说很好用,你怎么调用这个方法?
-
我在 js 文件中使用这个方法的路径...当我调试时我得到了 var json = null :/
-
那为什么不让方法正确异步呢?不需要
ContinueWith的东西。 -
那么,我该怎么办?
-
我的意思是this
标签: asp.net-mvc api http asp.net-web-api