C# 手动添加参数?

C# 手动添加参数?,c#,asp.net-core-mvc,swashbuckle,C#,Asp.net Core Mvc,Swashbuckle,Aspnet.Core上的SwashBulk通常从方法签名中读取所需的参数,例如 [HttpGet] [Route("/api/datasets/{id}")] [SwaggerOperation("DatasetsIdGet")] [SwaggerResponse(200, type: typeof(DataSet))] public IActionResult DatasetsIdGet([FromRoute]string id) { string exampleJson = nul

Aspnet.Core上的SwashBulk通常从方法签名中读取所需的参数,例如

[HttpGet]
[Route("/api/datasets/{id}")]
[SwaggerOperation("DatasetsIdGet")]
[SwaggerResponse(200, type: typeof(DataSet))]
public IActionResult DatasetsIdGet([FromRoute]string id)
{
    string exampleJson = null;

    var example = exampleJson != null ? JsonConvert.DeserializeObject<DataSet>(exampleJson) : default(DataSet);
    return new ObjectResult(example);
}
ID来自路由,可通过Swagger UI和生成的规范获得

不幸的是,我必须上传一些非常大的文件,并想禁用一个方法的formbinding

public async Task<IActionResult> Upload()
{
// drain fields manually. see https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads
// assume that there is the field bigupload.
}
使用Swagger编辑器,我可以很容易地描述这个场景——但我如何才能让Swashback相信这个方法将bigupload作为必填字段

编辑 下面是我基于swashbuckle github中的线程的解决方案

public class ImportFileParamType : IOperationFilter
{

    /// <summary>
    /// Adds formData Attributes to the Swagger Documentation.
    /// Must be registered in Startup.cs
    /// </summary>
    /// <param name="operation"></param>
    /// <param name="context"></param>
    public void Apply(Operation operation, OperationFilterContext context)
    {
        Console.WriteLine("ok");

        var attributes = context.ApiDescription.ActionAttributes()
        .OfType<SwaggerFormParameter>();

        foreach (var attribute in attributes)
        {
            if (operation.Parameters == null)
            {
                operation.Parameters = new List<IParameter>();
            }

            if (operation.Consumes.Count == 0)
            {
                operation.Consumes.Add("multipart/form-data");
            }

            var collectionFormat = attribute.CollectionFormat == CollectionFormat.None ? "" : attribute.CollectionFormat.ToString();

            operation.Parameters.Add(new NonBodyParameter()
            {
                Name = attribute.Name,
                Description = attribute.Description,
                In = "formData",
                Required = attribute.IsRequired,
                Type = attribute.Type,
                CollectionFormat = collectionFormat
            });
        }

        Console.WriteLine("ok");
    }
}

public enum CollectionFormat
{
    csv,
    ssv,
    tsv,
    pipes,
    None
}

/// <summary>
/// Adds pure FormData Objects to a Swagger Description. Useful if you cannot do Modelbinding because the uploaded Data is too large.
/// Set the type to "file" if you want files. Otherwise all supported primitve swagger-types should be ok.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public sealed class SwaggerFormParameter : Attribute
{
    public string Name { get; private set; }
    public string Type { get; private set; }
    public string Description { get; set; }
    public bool IsRequired { get; set; }

    public CollectionFormat CollectionFormat { get; set; }

    public SwaggerFormParameter(string name, string type)
    {
        Name = name;
        Type = type;
    }
}

您可以使用IOperationFilter实现这一点

公共类AddRequiredParameters:IOperationFilter { public void ApplyOperation操作,SchemaRegistry s,APIA描述 { 如果operation.operationId==ControllerName\u上传 { 如果operation.parameters==null operation.parameters=新列表; 操作.parameters.Add 新参数 { name=bigupload, @in=身体, @默认值=123, 类型=字符串, description=bla-bla, 必需=真 } ; } } }
下面是一个完整的示例:

您可以使用IOperationFilter来实现这一点

公共类AddRequiredParameters:IOperationFilter { public void ApplyOperation操作,SchemaRegistry s,APIA描述 { 如果operation.operationId==ControllerName\u上传 { 如果operation.parameters==null operation.parameters=新列表; 操作.parameters.Add 新参数 { name=bigupload, @in=身体, @默认值=123, 类型=字符串, description=bla-bla, 必需=真 } ; } } }
这里有一个完整的例子:

非常感谢!这是一个伟大的起点!我添加了一个自定义属性,该属性可以应用于方法,并由操作过滤器拾取。@ChristianSauer好主意!如果您可以共享该代码,以便其他人可以从中受益,那么使用自定义属性将消除operationId上的该条件。谢谢!这是一个伟大的起点!我添加了一个自定义属性,该属性可以应用于方法,并由操作过滤器拾取。@ChristianSauer好主意!如果您可以共享代码以使其他人受益,那么使用自定义属性将消除operationId上的该条件