【问题标题】:Example values(placeholder) for query parameters in Swagger (swashbuckle)Swagger (swashbuckle) 中查询参数的示例值(占位符)
【发布时间】:2022-01-18 14:32:51
【问题描述】:

我在 net core 中使用 swashbuckle 来创建一个大张旗鼓的文档化 API。

我的函数使用查询参数。我想在输入参数值的文本框旁边为每个查询参数添加一个简短的描述。我通过使用 XML cmets 实现了这一点:

/// <param name="propertyId">The integration property ID. Events related to presentations related to this ID will be returned</param>
public ActionResult GetEvents([BindRequired] string propertyId){}

这很好用,但它还在文本框占位符中添加了描述文本:

我看到如果生成的 JSON 具有查询参数的“示例”值,则可以更改占位符。如何使用 XML cmets 或类似的东西添加示例值或更改占位符?

【问题讨论】:

  • 所以你想要一个占位符值是吗?
  • 是的,或者至少要从文本框字段中删除描述文本并使其为空:) 现在它看起来像是由名称 + 描述组成

标签: xml asp.net-core parameters swagger swashbuckle


【解决方案1】:

您可以尝试将example="xxx" 添加到&lt;param&gt;&lt;/param&gt; 以添加示例值并更改占位符。

  /// <param name="propertyId" example="id">The integration property ID. Events related to presentations related to this ID will be returned</param>
        public IActionResult GetEvents([BindRequired] string propertyId) {
            ...
        }

结果:

【讨论】:

  • 非常感谢,我没想到会这么简单 :)
  • 还有一个问题,如果参数是DateTime类型怎么办?以这种方式在示例中设置字符串不起作用,唯一可行的可能性是 example = " " (将占位符设置为开始日期)和 example = "2021-12-12" (将其设置为 yyyy-MM- 中的日期时间ddTHH:mm:ss 格式)。有没有办法在这里使用字符串作为占位符?
  • 我意识到这确实提供了一个示例值,但它不会更改占位符。 prnt.sc/23a7e8y如果我点击试试看,文本值会在那里,我需要它是空的,带有自定义占位符
猜你喜欢
  • 2023-03-12
  • 1970-01-01
  • 2015-05-08
  • 1970-01-01
  • 2016-10-17
  • 1970-01-01
  • 2022-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多