2017-10-16 97 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

它像魅力一樣工作!謝謝 :) –