.Net Swagger Swashbuckle 5.0 为 POST 请求添加 Body 内容参数
在项目中创建一个 class,例如如下 BinaryPayloadFilter
的 class:
public class BinaryPayloadFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (context.ApiDescription.HttpMethod.ToUpper() != "POST")
return;
operation.RequestBody = new OpenApiRequestBody();
operation.RequestBody.Content.Add("application/json", new OpenApiMediaType()
{
Schema = new OpenApiSchema() { Type = "string" }
});
}
}
然后在 Startup.cs 文件中,相应位置加入如下代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.OperationFilter<BinaryPayloadFilter>(); // 加入该行代码
}
}
您的 Swagger 即可实现如下图效果: