简体   繁体   English

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

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

I have a Controller action method which reads the Request.Body directly (instead of using File) for streaming and other purposes.我有一个 Controller 操作方法,它直接(而不是使用文件)读取Request.Body用于流式传输和其他目的。 The problem is there is no model binding and therefore Swagger doesn't document the contract.问题是没有 model 绑定,因此 Swagger 没有记录合同。 For example:例如:

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

When loading Swagger UI, there is no way to upload a file, etc.加载Swagger UI时,无法上传文件等。

Is there any way to support this with attributes in ASP.NET Core?有没有办法通过 ASP.NET 核心中的属性来支持这一点?

The API: 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); ;  
    }  

Operation filter操作过滤器

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启动-ConfigureServices

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

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

The result in swagger UI: swagger UI 中的结果: 在此处输入图像描述

The source is: enter link description here来源是: 在此处输入链接描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从ASP.NET Core中的Request.Body中读取参数 - How to read parameter from Request.Body in ASP.NET Core asp.net 核心 1.0 mvc。 从 Request.Body 获取原始内容 - asp.net core 1.0 mvc. Get raw content from Request.Body 在 ASP.NET Core FromHeader 导致 Request.Body 被完全读取 - In ASP.NET Core FromHeader is causing the Request.Body to be fully read 从 ASP.NET Core 中的 Request.Body 读取数据时发生 SocketException - SocketException while reading data from Request.Body in ASP.NET Core ASP Net Core Web Api POST Request.Body始终为0 - Asp Net Core Web Api POST Request.Body always length 0 如何在 asp.net core webapi 控制器中读取请求正文? - How to read request body in an asp.net core webapi controller? 如何使 .NET 4.8 中的 Request.Content 在.Net Core 中工作? 或者 Request.Body 从.Net Core 到 .NET 4.8? - How to make Request.Content from .NET 4.8 to work in .Net Core? Or Request.Body from .Net Core to .NET 4.8? Swashbuckle + ASP.Net Core WebApi:Swagger 文档不包含用于版本选择的 Request-Header 或 QueryParameter? - Swashbuckle + ASP.Net Core WebApi: Swagger Document does not include Request-Header or QueryParameter for version selection? 如何在asp.net核心的IActionFilter中更改请求体 - How to change request body in IActionFilter in asp.net core Asp.Net Core重定向到操作但不允许直接调用操作? - Asp.Net Core redirect to action but not allow action to be called directly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM