当前位置:网站首页>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“

边栏推荐
- pyqt圖片解碼 編碼後加載圖片
- Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source
- TinyMCE visual editor adds Baidu map plug-in
- 20220702 how do programmers build knowledge systems?
- Market Research - current situation and future development trend of cell-based seafood market
- 关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
- [sword finger offer] 56 - I. the number of numbers in the array
- Les trois principaux points de douleur traités par servicemesh
- Five message formats of OSPF
- [shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
猜你喜欢

Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source

Ransack组合条件搜索实现
![[001] [arm-cortex-m3/4] internal register](/img/49/a0eceac1a67267216dd9b2566033a1.jpg)
[001] [arm-cortex-m3/4] internal register
![[shutter] shutter application theme (themedata | dynamic modification theme)](/img/77/6b0082368943aee7108ac550141f28.gif)
[shutter] shutter application theme (themedata | dynamic modification theme)
![[zero foundation I] Navicat download link](/img/23/e7808884152eeaf186fe756d6adac6.png)
[zero foundation I] Navicat download link

Hanoi Tower problem

From personal heroes to versatile developers, the era of programmer 3.0 is coming

From "bronze" to "King", there are three secrets of enterprise digitalization

Share how to make professional hand drawn electronic maps
![[shutter] shutter page Jump (route | navigator | page close)](/img/af/3fb2ca18bcec23a5c0c6897570fb53.gif)
[shutter] shutter page Jump (route | navigator | page close)
随机推荐
Bridge emqx cloud data to AWS IOT through the public network
APP页面分享口令Rails实现
[shutter] shutter layout component (wrap component | expanded component)
[shutter] shutter resource file use (import resource pictures | use image resources)
CVPR论文解读 | 弱监督的高保真服饰模特生成
Ransack组合条件搜索实现
Market Research - current market situation and future development trend of aircraft front wheel steering system
How to write a good program when a big book speaks every day?
Market Research - current situation and future development trend of cell-based seafood market
ServiceMesh主要解决的三大痛點
#include<>和#include“”的区别
How to prevent your jar from being decompiled?
Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
A specially designed loss is used to deal with data sets with unbalanced categories
100 important knowledge points that SQL must master: management transaction processing
: last child does not take effect
Technological Entrepreneurship: failure is not success, but reflection is
[sword finger offer] 56 - I. the number of numbers in the array
Market Research - current situation and future development trend of marine clutch Market
Reading experience of just because