Abp中SwaggerUI的接口文档添加上传文件参数类型

Abp中SwaggerUI的接口文档添加上传文件参数类型

2023年7月18日发(作者:)

Abp中SwaggerUI的接⼝⽂档添加上传⽂件参数类型在使⽤Swashbuckle上传⽂件的时候,在接⼝⽂档中希望看到上传控件,但是C#中,没有FromBodyAttribute这个特性,所以需要在运⾏时,修改参数的swagger属性。

⾸先看下,最终效果:

下⾯介绍实现。

实现原理,通过swagger提供的filter,找到action中带有SwaggerFileUpload特性的参数,然后给swagger ters添加⼀个⾃定义的参数,即⽂件类型参数即可。

(1)定义SwaggerFileUploadAttribute。[AttributeUsage(ter)]public class SwaggerFileUploadAttribute : Attribute{public bool Required { get; private set; }

public SwaggerFileUploadAttribute(bool Required = true){ed = Required;}}View Code

(2)添加Filter。///

/// swagger file upload parameter filter/// public class SwaggerFileUploadFilter : IOperationFilter{public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription){var parameters = ameters();foreach (HttpParameterDescriptor parameterDesc in parameters){var fileUploadAttr = tomAttributes().FirstOrDefault();if (fileUploadAttr != null){("multipart/form-data");

(new Parameter{name = terName + "_file",@in = "formData",description = "file to upload",required = ed,type = "file"});}}}}View Code

(3)给Swagger设置Filter。 (4)Action中的参数设置特性,测试。Public void TestSwaggerUploadFile([SwaggerFileUpload] file){ }

以上四部就可以实现⽂章开头的效果了。

发布者:admin,转转请注明出处:http://www.yc00.com/web/1689650327a273781.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信