我正在使用Swagger for WebApi 5.5.3 nuget包来获取API文档。在swagger UI中,它显示了可选参数的必需选项。如何将api参数标记为Web API 2的Swagger UI的可选参数?
我在Visual Studio中尝试了XML注释选项。下面是我想要记录的API方法:
/// <summary>
/// Gets the history.
/// </summary>
/// <param name="currentPageIndex">Index of the current page.</param>
/// <param name="pageSize">Size of the page.</param>
/// <param name="lastSyncDate">The last synchronize date.</param>
/// <returns></returns>
[HttpGet]
[Route("GetHistory/{currentPageIndex}/{pageSize}")]
public IHttpActionResult GetHistory(int currentPageIndex, int pageSize, DateTime? lastSyncDate)
{
var response = _myRepo.GetData();
if (response == null)
return BadRequest(Messages.InvalidPageIndex);
return Ok(response);
}
是表示lastSyncDate作为查询参数,但同时我也将其标记为可为空参数是必需的。
我也尝试使currentPageIndex在xml中可为空以及路由,但仍显示所需的所有属性。请帮忙。
只需将'= null'添加到最后一个参数。 – venerik
谢谢@venerik它真的帮助我。解决了这个问题。 –