欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用最小?WEB?API?實現(xiàn)文件上傳的Swagger支持

 更新時間:2022年02月20日 10:22:46   作者:My?IO  
這篇文章主要介紹了使用最小?WEB?API?實現(xiàn)文件上傳Swagger支持,我們使用最小?WEB?API?實現(xiàn)文件上傳功能,雖然客戶端訪問是正常的,但是當打開?Swagger?頁面時,沒法使用?Swagger?頁面測試,下面就來一篇支持Swagger的,需要的小伙伴可以參考一下

前言:

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

沒法使用 Swagger 頁面測試。

一、允許 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)屬性,唯獨沒有 ??file?? :

看來,只有自定義 Swagger 頁面了。

二、自定義 OperationFilter

在 OpenAPI 3.0 中,文件上傳的請求可以用下列結(jié)構(gòu)描述(https://swagger.io/docs/specification/describing-request-body/file-upload/):

而在 Swashbuckle 中,可以使用 IOperationFilter 接口實現(xiàn)操作篩選器,控制如何定義 Swagger UI 的行為。

在這里,我們將利用 ??RequestBody?? 對象來實現(xiàn)上述的文件上傳的請求結(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"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? };
? ? ? ? }
? ? }
}

然后,在啟動代碼中配置,應(yīng)用此操作篩選器:

builder.Services.AddSwaggerGen(setup =>
{
? ? setup.OperationFilter<FileUploadOperationFilter>();
});

這將呈現(xiàn)如下 Swagger 頁面:

到此這篇關(guān)于使用最小 WEB API 實現(xiàn)文件上傳的Swagger支持的文章就介紹到這了,更多相關(guān)使用最小 WEB API 實現(xiàn)文件上傳 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論