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

问题描述 投票:0回答:1

我创建了 .net core 最小 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

swagger swashbuckle
1个回答
0
投票

通过删除

.WithOpenApiOptions

方法,问题已解决,它显示参数名称并可与 Swagger UI 配合使用。

© www.soinside.com 2019 - 2024. All rights reserved.