【发布时间】:2021-11-10 19:47:22
【问题描述】:
我是 Asp.Net Core 的新手,我正在尝试构建一个 API。我有以下型号:
public class Location
{
public string Country { get; set; }
public string City { get; set; }
public string Street { get; set; }
}
我已经填充了我的数据库,并且一个国家有多个城市,而该城市有多个街道。 我创建了一个端点来从数据库中返回所有国家:
[HttpGet("countries")]
public async Task<ActionResult<IEnumerable<string>>> GetCountries()
{
return await _context.Location.Select(x=>x.Country).Distinct().ToListAsync();
}
这是返回字符串数组,我希望它返回以下 JSON 格式的响应:
[
{"country": "country1"},
{"country": "country2"},
..........
{"country": "countryN"}
]
我的第二个终点是检索特定国家/地区的所有城市:
[HttpGet("cities/{country}")]
public async Task<ActionResult<IEnumerable<string>>> GetCites(string country)
{
--------- Missing code ------
}
我尝试了不同的选项,但我只设法为两个端点获取字符串数组。我尝试用 IEnumerable 替换 IEnumerable,但没有结果。
【问题讨论】:
-
您在第二个 API 中寻找什么响应格式?和第一个 API 类似吗?
标签: linq asp.net-core entity-framework-core