【发布时间】:2016-09-13 05:42:25
【问题描述】:
我正在尝试使用 web api 从数据库表中返回 JSON。
控制器
private WebApiEntities dataContext = new WebApiEntities();
[ActionName("GetLocations")]
public IEnumerable<Location> GetLocations()
{
//IEnumerable<Location> list = null;
var list = (from c in dataContext.LocationMasters
select new Location
{
LocationId = c.LocationId,
LocationName = c.LocationName
}).ToList();
return list.OrderBy(c => c.LocationName);
}
位置模型:
public class Location
{
public int LocationId { get; set; }
public string LocationName { get; set; }
public string LocationImage { get; set; }
}
有人可以帮忙吗?如何返回带有父/节点的 JSON?当我运行上面的代码时,它会显示以下输出
[
{
"LocationId": 5,
"LocationName": "AMBERNATH",
"LocationImage": null
},
{
"LocationId": 1,
"LocationName": "BHIWANDI",
"LocationImage": null
},
{
"LocationId": 2,
"LocationName": "KALYAN",
"LocationImage": null
},
{
"LocationId": 3,
"LocationName": "THANE",
"LocationImage": null
},
{
"LocationId": 4,
"LocationName": "ULHASNAGAR",
"LocationImage": null
}
]
【问题讨论】:
-
你有什么问题?
-
你从哪里调用这个 api?
-
@CuongLe 我想要 Json 之前的父节点。有什么方法可以做到这一点?
-
@KarthikMR 直接通过浏览器调用。
-
这种情况是什么父节点?
标签: json asp.net-mvc asp.net-web-api asp.net-web-api2