【问题标题】:Including <returns> XML documentation block in Swagger Swashbuckle C#在 Swagger Swashbuckle C# 中包含 <returns> XML 文档块
【发布时间】:2021-12-23 23:50:25
【问题描述】:

如果我创建一个方法如下:

    /// <summary>
    /// Summary here
    /// </summary>
    /// <returns>THIS DOES NOT SHOW ANYWHERE</returns>
    /// <remarks>Remarks here</remarks>
    public async Task<string> MyMethod()
    {
        return "Hello World";
    }

并且我已经安装和设置了 Swashbuckle.AspNetCore,然后正确生成了文档,除了 &lt;returns&gt; 块中的值不会生成到 json 中的任何内容中:

"/api/v1.0/Exhibits/TestMethod1": {
      "get": {
        "tags": [
          "Blah"
        ],
        "summary": "Summary here",
        "description": "Remarks here",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              },
              "application/json": {
                "schema": {
                  "type": "string"
                }
              },
              "text/json": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    },

我怎样才能说服它把它导出到相关领域,或者这不可能?

【问题讨论】:

标签: c# swagger swagger-ui swashbuckle swashbuckle.aspnetcore


【解决方案1】:

define
完整记录了响应文档(端点返回) 每个状态代码的描述、内容类型、架构等

所以返回描述不同,取决于每个响应中的状态码。 所以你需要指定每个状态码的描述。

Swagger 使用一个或多个 &lt;response code="xxx"&gt; 而不是单个 &lt;returns&gt;

你的文档应该是这样的

/// <summary>
/// Retrieves a specific product by unique id
/// </summary>
/// <remarks>Awesomeness!</remarks>
/// <response code="200">Product created</response>
/// <response code="400">Product has missing/invalid values</response>
/// <response code="500">Oops! Can't create your product right now</response>
[HttpGet("{id}")]
[ProducesResponseType(typeof(Product), 200)]
[ProducesResponseType(typeof(IDictionary<string, string>), 400)]
[ProducesResponseType(500)]
public Product GetById(int id)

阅读How to add method description in Swagger UI in WebAPI Application了解更多

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    相关资源
    最近更新 更多