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

frebpwbc  于 2022-12-26  发布在  C#
关注(0)|答案(1)|浏览(149)

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

/// <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"
                }
              }
            }
          }
        }
      }
    },

我如何才能说服它将这一点输出到相关领域,或者这是不可能的?

vd8tlhqk

vd8tlhqk1#

返回描述是不同的,这取决于每个响应中的状态代码。因此您需要为每个状态代码指定什么是描述。
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)

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

相关问题