要做到這一點,你必須做兩件事情。
首先,你必須告訴揚鞭有在包含二進制數據的身體參數。接下來,你必須告訴Swagger終點消耗二進制數據(例如application/octet-stream)。
Swashbuckle不支持此開箱。但是您可以創建自定義過濾器來擴展Swashbuckle的功能。我通常做的是創建一個自定義屬性來裝飾一個方法,然後創建一個自定義過濾器來對該屬性進行操作。
你的情況,這會做的伎倆:
自定義屬性
public class BinaryPayloadAttribute : Attribute
{
public BinaryPayloadAttribute()
{
ParameterName = "payload";
Required = true;
MediaType = "application/octet-stream";
Format = "binary";
}
public string Format { get; set; }
public string MediaType { get; set; }
public bool Required { get; set; }
public string ParameterName { get; set; }
}
的自定義過濾器
public class BinaryPayloadFilter : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
var attribute = apiDescription.GetControllerAndActionAttributes<BinaryPayloadAttribute>().FirstOrDefault();
if (attribute == null)
{
return;
}
operation.consumes.Clear();
operation.consumes.Add(attribute.MediaType);
operation.parameters.Add(new Parameter
{
name = attribute.ParameterName,
@in = "body",
required = attribute.Required,
type = "string",
format = attribute.Format
});
}
}
過濾器添加到Swashbuckle配置
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
// other configuration setting removed for brevity
c.OperationFilter<BinaryPayloadFilter>();
});
應用屬性的方法
[HttpPost]
[BinaryPayload]
[Route("Store/{projectId}")]
public async Task Store(string projectId)
{
...
}
在揚鞭UI你再得到: