.net swashbuckle,文档端点查询参数,不在签名中

时间:2018-04-20 05:59:30

标签: c# .net swagger swashbuckle

我使用" message"中的查询参数,这些参数可以超过100并且是可选的。该信号应保持这种形式。

所以我的问题是,如何记录一些查询参数,以swagger UI显示,并保持可尝试?

/// <summary>
/// Callback Endpoint
/// </summary>
/// <returns>HTTP 200 <see cref="HttpStatusCode.OK"/>.</returns>
/// <param name="message">The message</param>
[HttpGet]
[SwaggerParameter("Something", "It is something")]
[Route("endpoint", Name = nameof(Endpoint))]
public virtual async Task<HttpResponseMessage> Endpoint(HttpRequestMessage message)

忽略&#34; HttpRequestMessage&#34;我使用OperationFilter: ASP.Net Web API Swashbuckle how to ignore HttpRequestMessage

config.EnableSwagger(swagger =>
            {
                swagger.OperationFilter<IgnoreHttpRequestMessage>();
                swagger.OperationFilter<SwaggerParameterAttributeHandler>();
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
    public class SwaggerParameterAttribute : Attribute
    {
        public SwaggerParameterAttribute(string name, string description)
        {
            Name = name;
            Description = description;
        }

        public string Name { get; private set; }

        public string Description { get; private set; }

        public bool Required { get; set; } = false;
    }
public class SwaggerParameterAttributeHandler : IOperationFilter
    {
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            // Get all SwaggerParameterAttributes on the method
            var attributes = apiDescription.ActionDescriptor.GetCustomAttributes<SwaggerParameterAttribute>(true);

            if (operation.parameters == null)
            {
                operation.parameters = new List<Parameter>();
            }

            foreach (var attribute in attributes)
            {
                var parameter = operation.parameters.FirstOrDefault(p => p.name == attribute.Name);

                if (parameter != null)
                {
                    parameter.required = attribute.Required;
                }
                else
                {
                    operation.parameters.Add(new Parameter()
                    {
                        name = attribute.Name,
                        description = attribute.Description,
                        type = "string",
                        required = attribute.Required
                    });
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我得到了一个观点,并得到了这个问题的解决方案。 我把这篇文章留在这里,也许有人需要它。

新参数的问题,“中”需要填写。在swaggerUI上,tryOut按钮完美运行。

operation.parameters.Add(new Parameter()
                    {
                        name = attribute.Name,
                        description = attribute.Description,
                        type = "string",
                        required = attribute.Required,
                        @in = "query"
                    });