如何大摇大摆地记录一个直接读取 Request.Body 的 ASP.NET 核心操作

How to swagger-document an ASP.NET Core action which reads Request.Body directly

我有一个控制器操作方法,它直接读取 Request.Body(而不是使用文件)用于流式传输和其他目的。问题是没有模型绑定,因此 Swagger 没有记录合同。例如:

[HttpPost("upload")]
[DisableFormValueModelBinding]
public async Task<IActionResult> UploadAsync()
{
  // Read from Request.Body manually, expecting content type to be multipart/*
  return Ok();
}

加载Swagger时UI,无法上传文件等

有什么方法可以在 ASP.NET Core 中使用属性来支持这一点?

API:

 [HttpPost]  
    public async Task<IActionResult> Post(  
        [FromForm(Name = "myFile")]IFormFile myFile)  
    {  
            using (var fileContentStream = new MemoryStream())  
            {  
                await myFile.CopyToAsync(fileContentStream);  
                await System.IO.File.WriteAllBytesAsync(Path.Combine(folderPath, myFile.FileName), fileContentStream.ToArray());  
            }  
            return CreatedAtRoute(routeName: "myFile", routeValues: new { filename = myFile.FileName }, value: null); ;  
    }  

运算过滤器

public class SwaggerFileOperationFilter : IOperationFilter  
{  
    public void Apply(Operation operation, OperationFilterContext context)  
    {  
        if (operation.OperationId == "Post")  
        {  
            operation.Parameters = new List<IParameter>  
            {  
                new NonBodyParameter  
                {  
                    Name = "myFile",  
                    Required = true,  
                    Type = "file",  
                    In = "formData"  
                }  
            };  
        }  
    }  
}  

Startup-ConfigureServices

services.AddSwaggerGen(  
    options =>  
    {  
        options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "My API", Version = "v1" });  

        options.OperationFilter<SwaggerFileOperationFilter>();  
    });  

招摇的结果UI:

来源是:enter link description here