333
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web.Http.Description;
|
||||
using Swashbuckle.Swagger;
|
||||
|
||||
namespace SwashbuckleEx.WebApiTest.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 添加 上传操作过滤
|
||||
/// </summary>
|
||||
public class AddUploadOperationFilter : IOperationFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// 重写Apply方法,加入Upload操作过滤
|
||||
/// </summary>
|
||||
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
|
||||
{
|
||||
var upload = apiDescription.ActionDescriptor.GetCustomAttributes<UploadAttribute>().FirstOrDefault();
|
||||
if (upload == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
operation.consumes.Add("application/form-data");
|
||||
if (operation.parameters==null)
|
||||
{
|
||||
operation.parameters = new List<Parameter>();
|
||||
}
|
||||
operation.parameters.Add(new Parameter()
|
||||
{
|
||||
name = upload.Name,
|
||||
@in = "formData",
|
||||
required = upload.Require,
|
||||
type = "file",
|
||||
description = upload.Description
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
|
||||
namespace SwashbuckleEx.WebApiTest.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 上传属性,用于标识接口是否包含上传信息参数
|
||||
/// </summary>
|
||||
public class UploadAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 参数名
|
||||
/// </summary>
|
||||
public string Name { get; set; } = "file";
|
||||
|
||||
/// <summary>
|
||||
/// 是否必须包含文件
|
||||
/// </summary>
|
||||
public bool Require { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
public string Description { get; set; } = "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user