2017-10-16 95 views
1
[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有三个可选参数将被传递作为查询字符串扬鞭 - 网页API - 可选查询参数

  1. SyncDate -
  2. 偏移龙 - 诠释
  3. 极限 - 诠释

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

我正在使用swashbuckle,我更喜欢使用注释,而不是在每个API方法上都有漫长的注释部分来描述Swagger功能。

我提到以下Adding Query String Params to my Swagger Specs过滤器创建的SwaggerParameterAttribute级Web API的文件夹,并试图给出添加OperationFilter G中lobalConfiguration.Configuration .EnableSwagger时,它抛出一个类型或无法找到名称空间名称SwaggerParametersAttributeHandler。我甚至还添加了筛选器文件夹命名空间,但仍存在错误。

请就如何落实在招摇

回答

1

可选查询参数指导扬鞭运作它的方式翻出参数根据你的动作IE参数,你行动的签名,但在这里,你是从ControllerContext获得这些值显然Swagger永远不会意识到。

所以你需要改变Action的签名并传递你的参数。

[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 
     . 
     . 
     . 

    } 
+0

谢谢,但在招摇的UI是非常需要的显示器 -

他们将作为可选的,如果你让他们可空类型的治疗。我如何将其更改为可选? –

+0

@TechJerk刚刚更新了我的答案,将空值分配给参数并使其成为可选参数。 –

+0

它像魅力一样工作!谢谢 :) –