当前位置:网站首页>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“
边栏推荐
- #include<>和#include“”的区别
- PHP微信抢红包的算法
- 基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
- Necessary browser plug-ins for network security engineers
- Pyqt picture decodes and encodes and loads pictures
- phpcms实现订单直接支付宝支付功能
- [shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
- Unity3D学习笔记4——创建Mesh高级接口
- Market Research - current market situation and future development trend of intravenous injection (IV) bottles
- [shutter] shutter layout component (wrap component | expanded component)
猜你喜欢
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
Off chip ADC commissioning record
phpcms实现订单直接支付宝支付功能
The failure rate is as high as 80%. What should we do about digital transformation?
Landingsite eband B1 smoke test case
Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
【零基础一】Navicat下载链接
C language, to achieve three chess games
What is it that makes you tremble? Those without fans can learn
随机推荐
The difference between include < > and include ""
Promise optimized callback hell
VictoriaMetrics 简介
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
CVPR论文解读 | 弱监督的高保真服饰模特生成
Market Research - current market situation and future development trend of aircraft audio control panel system
Necessary browser plug-ins for network security engineers
ServiceMesh主要解决的三大痛点
Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
: last child does not take effect
Daily book - low code you must understand in the era of digital transformation
情感计算与理解研究发展概述
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
Ransack组合条件搜索实现
Lightgbm principle and its application in astronomical data
记录一下微信、QQ、微博分享web网页功能
如何访问kubernetes API?
"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!
scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
Sql service intercepts string