Swagger-Web API-可选查询参数 [英] Swagger - Web API - Optional query parameters

查看:64
本文介绍了Swagger-Web API-可选查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[HttpGet]
[Route("students")]
[SwaggerOperation(Tags = new[] { "Student" })]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(ResponseModel<IList<Student>>))]
[SwaggerResponseExample(HttpStatusCode.OK, typeof(StudentResponseExample))]
[SwaggerResponse(HttpStatusCode.InternalServerError)]
public IHttpActionResult SearchStudent()
    {
        IDictionary<string, string> searchParams = null;
        searchParams = ControllerContext.GetQueryStrings();
        .
        .
        .

    }

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

The above API has three optional parameters which will be pass as query string

  1. SyncDate-长
  2. OffSet-int
  3. 限制-int

用户无法在swagger UI中输入这些可选的查询参数.请指导我实现可选的查询参数.

There is no option for user to enter these optional query parameters in swagger UI. Please guide me to implement the optional query parameters.

我正在使用swashbuckle,我更喜欢使用批注,而不是在每种API方法上都留有冗长的注释部分以了解更多功能.

I am using swashbuckle and I prefer to use annotations rather than having a lengthy comment section over each API method for swagger functionalities.

我引用了以下将查询字符串参数添加到我的Swagger规范并在Web API的 Filters 文件夹中以及尝试在G lobalConfiguration.Configuration中添加 OperationFilter 时创建 SwaggerParameterAttribute 类..EnableSwagger ,它引发类型或找不到命名空间名称 SwaggerParametersAttributeHandler .我什至添加了 Filters 文件夹名称空间,但仍然存在错误.

I referred the following Adding Query String Params to my Swagger Specs and created the SwaggerParameterAttribute class in Filters folder of Web API and when trying to add the OperationFilter in GlobalConfiguration.Configuration .EnableSwagger as given, it throws type or the namespace name SwaggerParametersAttributeHandler could not be found. I even added the Filters folder namespace but still the error exists.

请指导如何在swagger中实现可选查询参数

Please guide on how to implement the optional query parameters in swagger

推荐答案

Swagger的工作方式是基于Action的签名提取参数,即Action的参数,但是在这里,您从ControllerContext获得了这些值,显然Swagger会这样做永远不会意识到.

The way Swagger works it pulls out parameters based on your signature of Action i.e parameters to your Action, but here you are getting these value from ControllerContext which obviously Swagger will never be aware of.

因此,您需要更改Action的签名并在此处传递参数.

So You need to change the signature of the Action and pass your parameters there.

如果将它们设置为可空类型,它们将被视为可选-

They will be treated as optional if you make them of nullable type -

[HttpGet]
[Route("students")]
[SwaggerOperation(Tags = new[] { "Student" })]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(ResponseModel<IList<Student>>))]
[SwaggerResponseExample(HttpStatusCode.OK, typeof(StudentResponseExample))]
[SwaggerResponse(HttpStatusCode.InternalServerError)]
public IHttpActionResult SearchStudent(long? SyncDate = null,int? OffSet = null,int? Limit = null)
    {
        // Use the variables now here
        .
        .
        .

    }

这篇关于Swagger-Web API-可选查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆