首页 > 解决方案 > 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

标签: c#asp.net-web-apiswagger-uiswashbuckle.aspnetcore

解决方案


我不确定该Schema属性,但过去对我有用(设置typestring):

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

有关更多详细信息,请参阅此帖子


推荐阅读