Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

c# - 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.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...