Asp.net core Swashback.AspNetCore 5.0.0-rc4 UploadFileFilter不工作
我需要为Swashback.AspNetCore 5.0.0-rc4添加上传文件。在早期版本中,其工作原理如下:Asp.net core Swashback.AspNetCore 5.0.0-rc4 UploadFileFilter不工作,asp.net-core,swagger,swashbuckle,Asp.net Core,Swagger,Swashbuckle,我需要为Swashback.AspNetCore 5.0.0-rc4添加上传文件。在早期版本中,其工作原理如下: public class SwaggerUploadFileParametersFilter : IOperationFilter { public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) { if (op
public class SwaggerUploadFileParametersFilter : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
if (operation.parameters != null)
{
var attribute =
apiDescription.ActionDescriptor.GetCustomAttributes<UploadFileParametersAttribute>()
.FirstOrDefault();
if (attribute != null)
{
operation.consumes.Add("multipart/form-data");
operation.parameters.Add(new Parameter
{
name = "file",
required = true,
type = "file",
@in = "formData"
}
);
}
}
}
}
[UploadFileParameters]
public async Task<IHttpActionResult> MyMethod([FromUri]MyMethodParams parameters)
但它不起作用。我在swagger中没有看到文件组件我从中获取了您的代码和一些文档 我修改了你的代码并添加了小补丁 公共类SwiggerUploadFileParametersFilter:IOperationFilter { public void applyopenapio操作,操作筛选器上下文 { var参数=操作参数; 如果参数==null | |参数。计数==0 { 回来 } var isUploadFile=context.apisdescription.ActionDescriptor.Parameters.Anyx=>x.ParameterType==typeofIFormFile; 如果是上传文件 { operation.RequestBody=新的OpenApiRequestBody { 内容= { [多部分/表单数据]=新的OpenApiMediaType { Schema=newopenapischema { 类型=对象, 性质= { [文件]=新的OpenApiSchema { 描述=选择文件,类型=字符串,格式=二进制 } } } } } }; } } } 和控制器: [HttpPost] [产品响应类型StatusCodes.Status200OK] [ProductsDefaultResponseType] 公共异步任务上载文件异步[FromForm]格式文件
我从中获取了您的代码和一些文档 我修改了你的代码并添加了小补丁 公共类SwiggerUploadFileParametersFilter:IOperationFilter { public void applyopenapio操作,操作筛选器上下文 { var参数=操作参数; 如果参数==null | |参数。计数==0 { 回来 } var isUploadFile=context.apisdescription.ActionDescriptor.Parameters.Anyx=>x.ParameterType==typeofIFormFile; 如果是上传文件 { operation.RequestBody=新的OpenApiRequestBody { 内容= { [多部分/表单数据]=新的OpenApiMediaType { Schema=newopenapischema { 类型=对象, 性质= { [文件]=新的OpenApiSchema { 描述=选择文件,类型=字符串,格式=二进制 } } } } } }; } } } 和控制器: [HttpPost] [产品响应类型StatusCodes.Status200OK] [ProductsDefaultResponseType] 公共异步任务上载文件异步[FromForm]格式文件
我试过了,在大摇大摆的用户界面上,“选择文件”对话框窗口消失了;这与OpenAPI之前的更改非常混淆。第二,在调试时,当传入OperationFilter时,不会检测到ifformFile参数。operation.Parameters集合为空。我认为swagger在扫描时没有找到您的控制器,因为我使用的是同一个实现,可能是操作参数名称与[file]名称不对应。我尝试了此操作,在swagger UI上,选择文件对话框窗口消失;这与OpenAPI之前的更改非常混淆。第二,在调试时,当传入OperationFilter时,不会检测到ifformFile参数。operation.Parameters集合为空。我认为swagger在扫描时没有找到您的控制器,因为我使用的是同一个实现,可能操作参数名称与[file]名称不对应
public class SwaggerUploadFileParametersFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var actionAttributes = context.MethodInfo.GetCustomAttributes<UploadFileParametersAttribute>().FirstOrDefault();
if (actionAttributes != null)
{
operation.RequestBody = new OpenApiRequestBody()
{
Content =
{
["multipart/form-data"] = new OpenApiMediaType()
{
Schema = new OpenApiSchema()
{
Properties =
{
["file"] = new OpenApiSchema()
{
Description = "Select file",
Type = "file"
}
}
}
}
}
};
}
}
}