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

如何将api参数标记为Web api 2的Swagger UI的可选参数?,swagger,swagger-ui,swashbuckle,Swagger,Swagger Ui,Swashbuckle,我正在使用Swagger for WebApi 5.5.3 nuget包编写API文档。在swagger UI中,它显示可选参数的必需选项 我在VisualStudio中尝试了XML注释选项。下面是我要记录的API方法: /// <summary> /// Gets the history. /// </summary> /// <param name="currentPageIndex">Index of the curren

我正在使用Swagger for WebApi 5.5.3 nuget包编写API文档。在swagger UI中,它显示可选参数的必需选项

我在VisualStudio中尝试了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);
    }
//
///获取历史记录。
/// 
///当前页面的索引。
///页面的大小。
///上次同步日期。
/// 
[HttpGet]
[路由(“GetHistory/{currentPageIndex}/{pageSize}”)]
公共IHttpActionResult GetHistory(int currentPageIndex、int pageSize、DateTime?lastSyncDate)
{
var response=_myRepo.GetData();
如果(响应==null)
返回BadRequest(Messages.InvalidPageIndex);
返回Ok(响应);
}
它将lastSyncDate显示为查询参数,但它是必需的,因为我已将其标记为可为null的参数


我还尝试将currentPageIndex在xml和route中设置为null,但所有属性仍按要求显示。请提供帮助。

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

创建模型类

    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);
}

希望这能解决您的问题。

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

创建模型类

    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);
}

希望这能解决您的问题。

只需在最后一个参数中添加
=null
。谢谢@venerik,它真的帮助了我。解决了问题。只需在最后一个参数中添加
=null
。谢谢@venerik,它真的帮助了我。解决了这个问题。你能分享你的大摇大摆的截屏吗?你能分享你的大摇大摆的截屏吗?