当前位置:网站首页>It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
2022-07-02 22:26:00 【My space IO】
Preface
Last , We use the smallest WEB API Realize file upload function (《 It's not easy to say love you | Use minimum WEB API File upload 》), Although client access is normal , But when open Swagger When the page is , It turns out that :

Can't use Swagger Test page .
allow Content Type
natural Swagger The page should look like this :

It seems , We need to specify Content Type:
app.MapPost("/upload",
async (HttpRequest request) =>
{
var form = await request.ReadFormAsync();
return Results.Ok(form.Files.First().FileName);
}).Accepts<HttpRequest>("multipart/form-data");
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
result ,Swagger The page becomes like this , Added a pile of Form Related properties , Only No file :

It seems , Only custom Swagger Page .
Customize OperationFilter
stay OpenAPI 3.0 in , The request for file upload can be described with the following structure (https://swagger.io/docs/specification/describing-request-body/file-upload/):

And in the Swashbuckle in , have access to IOperationFilter Interface implements the action filter , Controls how Swagger UI act .
ad locum , We will use RequestBody Object to realize the above file upload request structure .
public class FileUploadOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
const string FileUploadContentType = "multipart/form-data";
if (operation.RequestBody == null ||
!operation.RequestBody.Content.Any(x =>
x.Key.Equals(FileUploadContentType, StringComparison.InvariantCultureIgnoreCase)))
{
return;
}
if (context.ApiDescription.ParameterDescriptions[0].Type == typeof(HttpRequest))
{
operation.RequestBody = new OpenApiRequestBody
{
Description = "My IO",
Content = new Dictionary<String, OpenApiMediaType>
{
{
FileUploadContentType, new OpenApiMediaType
{
Schema = new OpenApiSchema
{
Type = "object",
Required = new HashSet<String>{ "file" },
Properties = new Dictionary<String, OpenApiSchema>
{
{
"file", new OpenApiSchema()
{
Type = "string",
Format = "binary"
}
}
}
}
}
}
}
};
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
then , Configure... In the startup code , Apply this action filter :
builder.Services.AddSwaggerGen(setup =>
{
setup.OperationFilter<FileUploadOperationFilter>();
});
- 1.
- 2.
- 3.
- 4.
This will appear as follows Swagger page :

Conclusion
today , We use IOperationFilter Solved the minimum WEB API Realize file upload Swagger Support .
Want to know more about , Please pay attention to my official account ”My IO“

边栏推荐
- Off chip ADC commissioning record
- Technological Entrepreneurship: failure is not success, but reflection is
- [shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
- Introduction to victoriametrics
- [shutter] shutter resource file use (import resource pictures | use image resources)
- 攻防世界pwn题:Recho
- [leetcode] sword finger offer 11 Rotate the minimum number of the array
- LandingSite eBand B1冒烟测试用例
- 在beforeDestroy中销毁localStorage中的值无效
- SQL必需掌握的100个重要知识点:使用游标
猜你喜欢

Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation

"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
![[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)](/img/27/8594ba0b49d5008b7469967babed17.jpg)
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)

sql service 截取字符串

What is it that makes you tremble? Those without fans can learn

Hanoi Tower problem

Daily book -- analyze the pain points of software automation from simple to deep

情感计算与理解研究发展概述

scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文

Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
随机推荐
Daily book - low code you must understand in the era of digital transformation
Web侧防御指南
Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source
如何访问kubernetes API?
将 EMQX Cloud 数据通过公网桥接到 AWS IoT
2019 Nanchang (relive the classic)
Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
Service visibility and observability
New feature of go1.18: introduce new netip Network Library
发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...
《Just because》阅读感受
情感计算与理解研究发展概述
TinyMCE visual editor adds Baidu map plug-in
Promise optimized callback hell
【leetcode】1380. Lucky number in matrix
tinymce可视化编辑器增加百度地图插件
Market Research - current situation and future development trend of carob chocolate market
【剑指 Offer 】56 - II. 数组中数字出现的次数 II
[001] [arm-cortex-m3/4] internal register
PIP audit: a powerful security vulnerability scanning tool