当前位置:网站首页>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“
边栏推荐
- [shutter] shutter layout component (wrap component | expanded component)
- A week's life
- LandingSite eBand B1冒烟测试用例
- Kubernetes resource object introduction and common commands (4)
- GEE:(二)对影像进行重采样
- Technological Entrepreneurship: failure is not success, but reflection is
- *C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)
- Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
- How do I access the kubernetes API?
- Market Research - current situation and future development trend of preclinical medical device testing service market
猜你喜欢
#include<>和#include“”的区别
phpcms实现订单直接支付宝支付功能
Lightgbm principle and its application in astronomical data
地理探测器原理介绍
tinymce可视化编辑器增加百度地图插件
Evolution of messaging and streaming systems under the native tide of open source cloud
Etcd raft protocol
How to write a good program when a big book speaks every day?
Gee: (II) resampling the image
CVPR论文解读 | 弱监督的高保真服饰模特生成
随机推荐
[Jianzhi offer] 57 And are two numbers of S
Market Research - current situation and future development trend of preclinical medical device testing service market
SQL必需掌握的100个重要知识点:使用游标
The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
加了定位的文字如何水平垂直居中
ServiceMesh主要解决的三大痛點
Market Research - current situation and future development trend of marine clutch Market
Introduction to the principle of geographical detector
Sql service intercepts string
Market Research - current market situation and future development trend of aircraft front wheel steering system
地理探测器原理介绍
540. Single element in ordered array
Market Research - current market situation and future development trend of genome editing mutation detection kit
《Just because》阅读感受
From personal heroes to versatile developers, the era of programmer 3.0 is coming
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
Image segmentation using pixellib
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?