在Swagger Swashbuckle C#中包含<returns> XML文档块

3

如果我创建如下方法:

    /// <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,然后文档已经正确生成,但是在<returns>块中的值并没有生成成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"
                }
              }
            }
          }
        }
      }
    },

我该如何说服它将此导出到相关字段,或者这不可能吗?

使用XML文档将Swagger添加到ASP.NET Core Web API - stuartd
@stuartd那篇文章的作者也遇到了同样的问题:“如果你想知道返回的XML节点去哪了,我也不确定。如果有人能在评论中指出Swagger中它在哪里,我会很感激。” - GoldieLocks
哦,抱歉,我漏掉了那个。 - stuartd
我同意。如果没有 <response>,回退到 <returns> 会很好,或者以某种方式显示 <returns> 也可以。 - Patrick Szalapski
1个回答

4

返回描述因每个响应的状态码而异,因此您需要指定每个状态码的描述。

Swagger使用一个或多个<response code="xxx">而不是单个<returns>

您的文档应该是这样的

/// <summary>
/// Retrieves a specific product by unique id
/// </summary>
/// <remarks>Awesomeness!</remarks>
/// <response code="200">return Product with spacific id</response>
/// <response code="400">Product Not found</response>
/// <response code="500">Oops! Can't Found your product right now due to internal error</response>
[HttpGet("{id}")]
[ProducesResponseType(typeof(Product), 200)]
[ProducesResponseType(typeof(IDictionary<string, string>), 400)]
[ProducesResponseType(500)]
public Product GetById(int id)

阅读如何在WebAPI应用程序中添加Swagger UI方法描述获取更多信息。


有没有办法根本不显示 <returns>?例如,让它假设它是代码200,也许? - Patrick Szalapski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接