使用最小?WEB?API?實現(xiàn)文件上傳的Swagger支持
前言:
上回,我們使用最小 WEB API 實現(xiàn)文件上傳功能《? ?使用最小 WEB API 實現(xiàn)文件上傳會遇到的坑??》,雖然客戶端訪問是正常的,但是當打開 Swagger 頁面時,發(fā)現(xiàn)是這樣的:

沒法使用 Swagger 頁面測試。
一、允許 Content Type
正常的 Swagger 頁面應該是這樣的:

看來,我們需要指定 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");結果,Swagger 頁面變成了這樣,增加了一堆 Form 相關屬性,唯獨沒有 ??file?? :

看來,只有自定義 Swagger 頁面了。
二、自定義 OperationFilter
在 OpenAPI 3.0 中,文件上傳的請求可以用下列結構描述(https://swagger.io/docs/specification/describing-request-body/file-upload/):

而在 Swashbuckle 中,可以使用 IOperationFilter 接口實現(xiàn)操作篩選器,控制如何定義 Swagger UI 的行為。
在這里,我們將利用 ??RequestBody?? 對象來實現(xiàn)上述的文件上傳的請求結構。
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"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? };
? ? ? ? }
? ? }
}然后,在啟動代碼中配置,應用此操作篩選器:
builder.Services.AddSwaggerGen(setup =>
{
? ? setup.OperationFilter<FileUploadOperationFilter>();
});這將呈現(xiàn)如下 Swagger 頁面:

到此這篇關于使用最小 WEB API 實現(xiàn)文件上傳的Swagger支持的文章就介紹到這了,更多相關使用最小 WEB API 實現(xiàn)文件上傳 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Entity?Framework使用配置伙伴創(chuàng)建數(shù)據(jù)庫
這篇文章介紹了Entity?Framework使用配置伙伴創(chuàng)建數(shù)據(jù)庫的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-03-03
asp.net 合并GridView中某列相同信息的行(單元格)
合并GridView中某列相同信息的行(單元格)2009-11-11
.Net?Core授權認證方案JWT(JSON?Web?Token)初探
這篇文章介紹了.Net?Core授權認證方案JWT,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06
.net實現(xiàn)oracle數(shù)據(jù)庫中獲取新插入數(shù)據(jù)的id的方法
在oracle數(shù)據(jù)庫中實現(xiàn)插入數(shù)據(jù)的自動增長不是很容易,想在.net中實現(xiàn)獲取新插入數(shù)據(jù)的id,感興趣的朋友看下詳細的解決方法,希望對你有所幫助2013-04-04
HttpRequest的QueryString屬性 的一點認識
我們開發(fā)asp.net程序獲取QueryString時,經(jīng)常性的遇到一些url編碼問題2012-11-11
net core webapi多版本控制與swagger(nswag)配置教程
這篇文章主要介紹了net core webapi多版本控制與swagger(nswag)配置,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11

