在Swagger中设置示例

时间:2018-07-05 22:01:18

标签: asp.net-core swagger swashbuckle

我正在使用Swashbuckle.AspNetCore.Swagger(1.0.0)和Swashbuckle.AspNetCore.SwaggerGen(1.0.0)。我正在尝试在Default model example in Swashbuckle (Swagger)之后向我的API添加默认示例。我创建了一个新的类文件并添加了

public class SwaggerDefaultValue : Attribute
{
    public string ParameterName { get; set; }
    public string Value { get; set; }

    public SwaggerDefaultValue(string parameterName, string value)
    {
        this.ParameterName = parameterName;
        this.Value = value;
    }
}

public class AddDefaultValues : IOperationFilter
{
    public void Apply(Operation operation, DataTypeRegistry dataTypeRegistry, ApiDescription apiDescription)
    {
        foreach (var param in operation.Parameters)
        {
            var actionParam = apiDescription.ActionDescriptor.GetParameters().First(p => p.ParameterName == param.Name);

            if (actionParam != null)
            {
                var customAttribute = actionParam.ActionDescriptor.GetCustomAttributes<SwaggerDefaultValue>().FirstOrDefault();
                if (customAttribute != null)
                {
                    param.DefaultValue = customAttribute.Value;
                }
            }
        }
    }
}

但我收到此错误-AddDefaultValues does not implement interface member IOperationFilter.Apply(Operation, OperationFilterContext)

1 个答案:

答案 0 :(得分:0)