如何将api参数标记为Web API 2的Swagger UI的可选参数?

时间:2016-12-27 09:04:01

标签: swagger swagger-ui swashbuckle

我正在使用Swagger for WebApi 5.5.3 nuget包来获取API文档。在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中的可空和路由,但仍然按要求显示所有属性。请帮忙。

1 个答案:

答案 0 :(得分:0)

下面给出此问题的解决方案

创建模型类

    using System;
    using System.ComponentModel.DataAnnotations;

    public class SearchHistory
    {
      [Required]
      public int CurrentPageIndex { get; set; }
      [Required]
      public int PageSize { get; set; }
      public DateTime? LastSyncDate { get; set; }
    }

使用新建模型

更改输入参数
 [HttpGet]
 public IHttpActionResult GetHistory(SearchHistory modle)
{
    var response = _myRepo.GetData();
    if (response == null)
        return BadRequest(Messages.InvalidPageIndex);

    return Ok(response);
}

希望这能解决您的问题。

相关问题