【发布时间】:2021-08-06 04:43:40
【问题描述】:
我正在使用 .NetCore 3 和 Swagger 5.0.0-rc4。我正在尝试使用 Swagger 上传文件(图像),但它不起作用,因为 IOperationFilter 甚至 Swashbuckle.AspNetCore.Swagger 中的应用方法缺少一些属性。例如 NonBodyParameter 和 Consumes 不会在 Swagger 5.0 中退出
有没有人遇到同样的问题或试图解决它?
public class FileOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.OperationId.ToLower() == "apivaluesuploadpost")
{
operation.Parameters.Clear();
operation.Parameters.Add(new **NonBodyParameter**
{
Name = "uploadedFile",
In = "formData",
Description = "Upload File",
Required = true,
Type = "file"
});
operation.**Consumes**.Add("multipart/form-data");
}
}
}
【问题讨论】:
-
Please do not post images of code。相反edit您的问题并将代码添加为格式正确的markdown
-
你能澄清一下文件上传是如何不起作用的吗?预期结果和实际结果是什么?
-
要使 File-upload 在 Swagger 中工作,您必须对 FileOperationFilter 进行一些自定义并在 StartUp 类中进行配置,但这种自定义无法在 Swagger 5.0 中实现,因为似乎删除了一些属性来自 Swagger 5.0。我在没有自定义的情况下实现 IFormFile 时得到的结果如下: Fetch errorundefined /swagger/v1/swagger.json
-
我遇到了同样的问题;自升级以来,选择文件对话框按钮已消失。所以不知道如何测试和验证它是否正常工作。
标签: c# asp.net-core file-upload swagger .net-core-3.0