Swagger - Web API - 可选的查询参数

时间:2017-10-16 07:13:03

标签: c# asp.net-web-api swagger swashbuckle

DECLARE @DistinctFilteringCriteria TINYINT;

SELECT @DistinctFilteringCriteria = COUNT(DISTINCT [vcFilterType])
FROM @FilterDataSource;

WITH DataSource AS
(
    SELECT DS.*
          ,COUNT([vcDemographicType]) OVER (PARTITION BY [biPostId]) AS [FilteringCriteriaMatched]
    FROM @DataSource DS
    INNER JOIN @FilterDataSource FDS
        ON DS.[vcDemographicType] = FDS.[vcFilterType]
        AND DS.[vcDemographicValue] = FDS.[vcFilterValue]
)
SELECT [biDemographicId], [biPostId], [vcDemographicType], [vcDemographicValue]
FROM DataSource
WHERE [FilteringCriteriaMatched] = @DistinctFilteringCriteria; 

上述API有三个可选参数,将作为查询字符串传递

  1. SyncDate - Long
  2. OffSet - int
  3. Limit - int
  4. 用户无法在swagger UI中输入这些可选查询参数。请指导我实现可选的查询参数。

    我正在使用swashbuckle,我更喜欢使用注释,而不是对每个API方法都有冗长的评论部分,以便实现swagger功能。

    我引用了以下Adding Query String Params to my Swagger Specs并在Web API的过滤器文件夹中创建了 SwaggerParameterAttribute 类,并尝试添加 OperationFilter 在G lobalConfiguration.Configuration中                 .EnableSwagger 如给定,它抛出类型或命名空间名称 SwaggerParametersAttributeHandler 找不到。我甚至添加了过滤器文件夹命名空间,但仍然存在错误。

    请指导如何在swagger

    中实现可选查询参数

2 个答案:

答案 0 :(得分:5)

Swagger的工作方式是根据你的Action的签名,即你的Action的参数来提取参数,但是你在这里从ControllerContext获得这些值,这显然是Swagger永远不会知道的。

所以你需要更改Action的签名并在那里传递你的参数。

如果您将它们设为可为空的类型 -

,它们将被视为可选项
void create()
{
int random;
for (int i = 0; i < 10; i++)
{
    struct node *new_node, *current;
    new_node = new node;
    random = randomNum();
    new_node->data = random;
    new_node->next = NULL;
    if (start == NULL)
    {
        start = new_node;
        current = new_node;
        new_node = NULL;
    }
    else
    {
        current->next = new_node;
        current = new_node;
    }
 }
}

void display()
{
struct node *new_node;
new_node = start;
while (new_node != NULL)
{
    cout << new_node->data << "->";
    new_node = new_node->next;
}
}

答案 1 :(得分:1)

这对我有用:

[System.Web.Http.HttpGet] 
[Route("api/DoStuff/{reqParam}")]  
[Route("api/DoStuff/{reqParam}/{optParam1:alpha?}/{optParam2:datetime?}")]
public string Get(string reqParam, string optParam1= "", string optParam2= "")

它确实在Swagger用户界面中创建了两个部分,但对我有用。

相关问题