C# IActionResult vs ActionResult<;T>;vs T-为什么';行动结果<;T>;工作
我正在处理一个.NET 5 web api,下面的代码在我尝试返回C# IActionResult vs ActionResult<;T>;vs T-为什么';行动结果<;T>;工作,c#,asp.net-core,asp.net-core-webapi,C#,Asp.net Core,Asp.net Core Webapi,我正在处理一个.NET 5 web api,下面的代码在我尝试返回NotFound()时抛出了一个错误 有人知道为什么我的第一个方法不起作用吗?好吧……以防其他人遇到这种情况。问题是FileResultStream是一个ActionResult,因此在此场景中使用ActionResult是不正确的,其中T是另一个ActionResult。我正在使用IActionResult,然后将我的属性更新为: [产品响应类型(typeof(FileStreamResult),StatusCodes.Stat
NotFound()
时抛出了一个错误
有人知道为什么我的第一个方法不起作用吗?好吧……以防其他人遇到这种情况。问题是
FileResultStream
是一个ActionResult
,因此在此场景中使用ActionResult
是不正确的,其中T是另一个ActionResult
。我正在使用IActionResult
,然后将我的属性更新为:
[产品响应类型(typeof(FileStreamResult),StatusCodes.Status200OK)]
如果有更好的方法,请告诉我。您可能也希望从屏幕截图中省略查询……我认为这是因为您使用的是作为T的操作结果类型。在第二秒内,您使用的是作为数据返回的模型。@IanKemp只是在实际代码中省略以消除一些干扰。查询中没有真正的秘密。谢谢你。
[ApiController]
public class Download : ControllerBase
{
// Constructor and members omitted...
[HttpGet( Routes.KatApps.Download )]
[SwaggerOperation(
Summary = "Find and download KatApp kaml file based on precedence of folders passed in",
Description = "Find and download KatApp kaml file based on precedence of folders passed in",
OperationId = "KatApps." + nameof( Routes.KatApps.Download ),
Tags = new[] { "KatApps" } )]
[ProducesResponseType( StatusCodes.Status200OK )]
[ProducesResponseType( StatusCodes.Status304NotModified )]
[ProducesResponseType( typeof( ValidationProblemDetails ), StatusCodes.Status401Unauthorized )]
[ProducesResponseType( typeof( ValidationProblemDetails ), StatusCodes.Status404NotFound )]
public async Task<ActionResult<FileStreamResult>> HandleAsync( [FromQuery] Parameters parameters )
{
using ( var cn = await dbConnectionFactory.CreateDataLockerConnectionAsync() )
{
foreach ( var folder in parameters.Folder )
{
var keyInfo = await cn.QueryBuilder( $@"Query Omitted" ).QueryFirstOrDefaultAsync<CacheDownloadInfo>();
if ( keyInfo != null )
{
return await CachedOrModifiedAsync( keyInfo, dbConnectionFactory );
}
}
return NotFound();
}
}
}
protected async Task<FileStreamResult> CachedOrModifiedAsync( CacheDownloadInfo cacheDownloadInfo, IDbConnectionFactory dbConnectionFactory )
{
// Code to return FileStreamResult
}
public async Task<ActionResult<ManagedFileInfo>> HandleAsync( [FromQuery] Parameters parameters )
{
using ( var cn = await dbConnectionFactory.CreateDataLockerConnectionAsync() )
{
var liveFiles = ( await QueryFileVersions( cn, new[] { parameters.Name }, parameters.Folder ) ).ToArray();
if ( liveFiles.Length == 0 )
{
return NotFound();
}