使用.net core最小API上传文件,Swagger不显示参数名称

vdgimpew  于 9个月前  发布在  .NET
关注(0)|答案(1)|浏览(150)

我已经创建了.net核心最小的API来上传文件使用.net 7,Swashbuckle 6.5。API在没有Swagger的情况下工作,并使用Postman进行了测试。
使用Swashbuckle添加OpenAPI/Swagger支持,它会生成测试页面,但文件上传参数名称不会显示在Swagger中,并且尝试上传没有名称的文件会导致以下错误。
该问题在Swashbuckle GitHub问题跟踪器中报告。https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2625
有什么变通方法吗
Microsoft.AspNetCore.Http.BadHttpRequestException: Required parameter "IFormFile file" was not provided from form file.

app.MapPost("/ReadPDF", async (IFormFile file) =>
{
    string tempfile = CreateTempfilePath();
    using var stream = File.OpenWrite(tempfile);

    await file.CopyToAsync(stream);
    
    stream.Close();

    StringBuilder sbPDFText=new StringBuilder();
    

    return Text.ToString();

})
    .WithName("ReadPDF")
.WithOpenApi(operation => new(operation)
{

    Summary = "Reads text from PDF",
    Description = "This API returns all text in PDF document"
});
;

我已经尝试了GitHub页面上提到的解决方案作为解决方案,但无法解决。https://github.com/dotnet/aspnetcore/issues/47692

92vpleto

92vpleto1#

通过移除
.WithOpenApiOptions
的方法,问题是固定的,它的显示参数名称,并与Swagger用户界面。

相关问题