[英]ActionResult<IEnumerable<T>> has to return a List<T>
Take the following code using ASP.NET Core 2.1: 使用ASP.NET Core 2.1获取以下代码:
[HttpGet("/unresolved")]
public async Task<ActionResult<IEnumerable<UnresolvedIdentity>>> GetUnresolvedIdentities()
{
var results = await _identities.GetUnresolvedIdentities().ConfigureAwait(false);
return results.ToList();
}
I would have thought since GetUnresolvedIdentities()
returns IEnumerable<UnresolvedIdentity>
that I could just return 我会想到,因为
GetUnresolvedIdentities()
返回IEnumerable<UnresolvedIdentity>
,我可以返回
return await _identities.GetUnresolvedIdentities().ConfigureAwait(false);
Except I can't, as I get this error: 除了我不能,因为我得到这个错误:
CS0029 Cannot implicitly convert type
'System.Collections.Generic.IEnumerable<Data.Infrastructure.Models.UnresolvedIdentity>'
to'Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.Generic.IEnumerable<Data.Infrastructure.Models.UnresolvedIdentity>>'
CS0029无法将类型
'System.Collections.Generic.IEnumerable<Data.Infrastructure.Models.UnresolvedIdentity>'
隐式转换为'Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.Generic.IEnumerable<Data.Infrastructure.Models.UnresolvedIdentity>>'
I need the .ToList()
, which is annoying as it's 2 lines rather than 1. 我需要
.ToList()
,这很烦人,因为它是2行而不是1行。
Why can't ActionResult<T>
figure out that GetUnresolvedIdentities()
returns an IEnumerable<>
and just return that? 为什么不能
ActionResult<T>
弄清楚GetUnresolvedIdentities()
返回一个IEnumerable<>
并返回那个?
The signature of GetUnresolvedIdentities
is: GetUnresolvedIdentities
的签名是:
Task<IEnumerable<UnresolvedIdentity>> GetUnresolvedIdentities();
Take this documentation from msdn: https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-2.1#actionresultt-type 从msdn获取此文档: https ://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types ? view = aspnetcore-2.1#actionresultt-type
C# doesn't support implicit cast operators on interfaces.
C#不支持接口上的隐式转换运算符。 Consequently, conversion of the interface to a concrete type is necessary to use
ActionResult<T>
.因此,使用
ActionResult<T>
需要将接口转换为具体类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.