【发布时间】:2022-01-16 23:02:53
【问题描述】:
为什么这个功能不起作用:
[HttpGet("getCustomerAsDict")]
public async Task<object> GetCustomersAsDict()
{
var customOutPut = new Dictionary<int, string>();
var customer = await _context.Customers.ToListAsync();
foreach (var custom in customer)
{
customOutPut[custom.CustomerId] = custom.CustomerName;
}
return customOutPut;
}
邮递员投掷: System.NotSupportedException:不支持集合类型“System.Collections.Generic.Dictionary`2[System.Int32,System.String]”。
状态:500 内部服务器错误
但是这个功能确实有效:
[HttpGet("getCustomerAsDict")]
public async Task<object> GetCustomersAsDict()
{
var customOutPut = new Dictionary<string, int>();
var customer = await _context.Customers.ToListAsync();
foreach (var custom in customer)
{
customOutPut[custom.CustomerName] = custom.CustomerId;
}
return customOutPut;
}
邮递员给了我我需要的答案
【问题讨论】:
-
我刚刚测试过。一切正常。你用的是什么序列化器?你能用控制器配置展示你的 API 启动吗
-
IMO int->字符串映射使 JSON 无效,因为字典/对象意味着具有字符串键/标识符。
-
另一方面,我没有任何确凿的证据表明端点会返回 JSON。
标签: c# dictionary httpclient http-get