ActionResult <IEnumerable <T >>必须返回一个List <T>

斯图尔特

使用ASP.NET Core 2.1进行以下代码:

[HttpGet("/unresolved")]
public async Task<ActionResult<IEnumerable<UnresolvedIdentity>>> GetUnresolvedIdentities()
{
   var results = await _identities.GetUnresolvedIdentities().ConfigureAwait(false);
   return results.ToList();
}

自从GetUnresolvedIdentities()退货后IEnumerable<UnresolvedIdentity>,我本以为可以退货

return await _identities.GetUnresolvedIdentities().ConfigureAwait(false);

除了我不能,因为出现此错误:

CS0029无法将类型隐式转换'System.Collections.Generic.IEnumerable<Data.Infrastructure.Models.UnresolvedIdentity>''Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.Generic.IEnumerable<Data.Infrastructure.Models.UnresolvedIdentity>>'

我需要.ToList(),这很烦人,因为它是2行而不是1行。

为什么不能ActionResult<T>弄清楚GetUnresolvedIdentities()返回一个,IEnumerable<>然后返回那个呢?

的签名GetUnresolvedIdentities是:

Task<IEnumerable<UnresolvedIdentity>> GetUnresolvedIdentities();
V0ldek

从msdn中获取此文档:https ://docs.microsoft.com/zh-cn/aspnet/core/web-api/action-return-types ? view = aspnetcore-2.1#actionresultt-type

C#不支持接口上的隐式强制转换运算符。因此,必须使用将接口转换为具体类型的方法ActionResult<T>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章