Ter*_*rry 4 c# asp.net-core asp.net-core-webapi
我正在开发 .NET 5 Web api,当我尝试返回 .NET 5 Web api 时,以下代码会引发错误NotFound()。
[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
}
Run Code Online (Sandbox Code Playgroud)
但当我拨打电话时NotFound,我得到:
System.ArgumentException:为“ActionResult<T>”指定的类型参数“Microsoft.AspNetCore.Mvc.FileStreamResult”无效。
奇怪的是,我有另一个控制器操作(跳过所有设置代码以仅显示签名并启动),当我返回时效果很好NotFound()。
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();
}
Run Code Online (Sandbox Code Playgroud)
有人知道为什么我的第一种方法不起作用吗?
Doh...以防其他人遇到这种情况。问题是FileResultStream an,因此在这种情况下使用 T 是 anotherActionResult是不正确的。我正在使用然后将我的属性更新为:ActionResult<T>ActionResultIActionResult
[ProducesResponseType( typeof( FileStreamResult ), StatusCodes.Status200OK )]
如果有更好的方法,请告诉我。