使用最小?WEB?API?實(shí)現(xiàn)文件上傳的Swagger支持
前言:
上回,我們使用最小 WEB API 實(shí)現(xiàn)文件上傳功能《? ?使用最小 WEB API 實(shí)現(xiàn)文件上傳會(huì)遇到的坑??》,雖然客戶端訪問是正常的,但是當(dāng)打開 Swagger 頁面時(shí),發(fā)現(xiàn)是這樣的:
沒法使用 Swagger
頁面測(cè)試。
一、允許 Content Type
正常的 Swagger 頁面應(yīng)該是這樣的:
看來,我們需要指定 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");
結(jié)果,Swagger 頁面變成了這樣,增加了一堆 Form 相關(guān)屬性,唯獨(dú)沒有 ??file?? :
看來,只有自定義 Swagger 頁面了。
二、自定義 OperationFilter
在 OpenAPI 3.0 中,文件上傳的請(qǐng)求可以用下列結(jié)構(gòu)描述(https://swagger.io/docs/specification/describing-request-body/file-upload/):
而在 Swashbuckle
中,可以使用 IOperationFilter
接口實(shí)現(xiàn)操作篩選器,控制如何定義 Swagger UI
的行為。
在這里,我們將利用 ??RequestBody
?? 對(duì)象來實(shí)現(xiàn)上述的文件上傳的請(qǐng)求結(jié)構(gòu)。
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" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }; ? ? ? ? } ? ? } }
然后,在啟動(dòng)代碼中配置,應(yīng)用此操作篩選器:
builder.Services.AddSwaggerGen(setup => { ? ? setup.OperationFilter<FileUploadOperationFilter>(); });
這將呈現(xiàn)如下 Swagger 頁面:
到此這篇關(guān)于使用最小 WEB API 實(shí)現(xiàn)文件上傳的Swagger支持的文章就介紹到這了,更多相關(guān)使用最小 WEB API 實(shí)現(xiàn)文件上傳 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用擴(kuò)展方法優(yōu)化多條件查詢(不定條件查詢)
在我們開發(fā)過程中,特別是管理系統(tǒng)的開發(fā),經(jīng)常會(huì)遇到多條件查詢(或者叫不定條件查詢)的案例,就是提供給User輸入的查詢條件有多個(gè)不同的查詢欄位,而且,在實(shí)際使用中并不能確定User會(huì)使用哪些條件來當(dāng)做搜索條件2012-12-12Entity?Framework使用配置伙伴創(chuàng)建數(shù)據(jù)庫
這篇文章介紹了Entity?Framework使用配置伙伴創(chuàng)建數(shù)據(jù)庫的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03asp.net 合并GridView中某列相同信息的行(單元格)
合并GridView中某列相同信息的行(單元格)2009-11-11.Net?Core授權(quán)認(rèn)證方案JWT(JSON?Web?Token)初探
這篇文章介紹了.Net?Core授權(quán)認(rèn)證方案JWT,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06.net實(shí)現(xiàn)oracle數(shù)據(jù)庫中獲取新插入數(shù)據(jù)的id的方法
在oracle數(shù)據(jù)庫中實(shí)現(xiàn)插入數(shù)據(jù)的自動(dòng)增長(zhǎng)不是很容易,想在.net中實(shí)現(xiàn)獲取新插入數(shù)據(jù)的id,感興趣的朋友看下詳細(xì)的解決方法,希望對(duì)你有所幫助2013-04-04HttpRequest的QueryString屬性 的一點(diǎn)認(rèn)識(shí)
我們開發(fā)asp.net程序獲取QueryString時(shí),經(jīng)常性的遇到一些url編碼問題2012-11-11net core webapi多版本控制與swagger(nswag)配置教程
這篇文章主要介紹了net core webapi多版本控制與swagger(nswag)配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11