简体   繁体   中英

ASP.NET Web Api Swagger string parameters error - no description

I am using Swagger UI to test my ASP.NET Web Api app. I added a class to allow operation parameters

public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
    if (operation.Parameters == null) 
        operation.Parameters = new List<OpenApiParameter>();

    operation.Parameters.Add(new OpenApiParameter
    {
        Name = "ApiKey",
        In = ParameterLocation.Header,
        Required = true,
        Schema = new OpenApiSchema
        {
            Type = "String"
        }
    });
    operation.Parameters.Add(new OpenApiParameter
    {
        Name = "Authentication",
        In = ParameterLocation.Header,
        Required = false,
        Schema = new OpenApiSchema
        {
            Type = "String"
        }
    });
}

In my Startup.cs, I added this line to the ConfigurationServices method

c.OperationFilter<CustomHeaderSwaggerAttribute>();

When I try and test one of the controller methods, my ApiKey string parameter always show an error no matter what I put in the textbox.

在此处输入图像描述

I am not sure about the Schema property but the following worked for me in past (setting the type to string ):

operation.Parameters.Add(new Parameter
        {
            name = "ApiKey",
            @in = ParameterLocation.Header,
            required = true,
            type = "string"                
        });

For more details, refer to this post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM