【问题标题】:Swagger API not refreshing the documentationSwagger API 不刷新文档
【发布时间】:2017-03-15 17:21:24
【问题描述】:

我正在使用 Swagger API 来记录我的 REST 服务。 早些时候我的控制器方法没有信息丰富的 cmets,因此 Swagger API 没有显示描述,但现在即使在更新 cmets 之后,我也没有在突出显示的区域中获得方法描述。

    /// <summary>
    /// Gets the consumer scores by retailer id and return id
    /// </summary>
    /// <param name="retailerId"></param>
    /// <param name="returnId"></param>
    /// <returns></returns>

我错过了什么吗?

【问题讨论】:

  • 当您说“使用 swagger API”时,您的意思是什么?您具体使用什么库从 C# 生成您的 swagger 文档?
  • 我将 Swashbuckle.AspNetCore 与我的 C# Web API 一起使用。

标签: c# asp.net-web-api swagger swashbuckle


【解决方案1】:

为了让 Swashbuckle 从您的 XML cmets 中读取数据,您需要为您的目标项目启用 XML 文档文件。除此之外,您还需要在启动配置中将 Swashbuckle 指向该文件。

来自Swashbuckle Documentation

打开项目的“属性”对话框,单击“构建”选项卡并 确保选中“XML 文档文件”。这将产生一个 包含构建时所有 XML cmets 的文件。

此时,任何未使用 XML 注释的类或方法 cmets 将触发构建警告。要抑制这种情况,请输入 警告代码“1591”到“禁止警告”字段中 属性对话框。*

配置 Swashbuckle 以将文件中的 XML cmets 合并到 生成 Swagger JSON:

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1",
        new Info
        {
            Title = "My API - V1",
            Version = "v1"
        }
     );

     var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "MyApi.xml");
     c.IncludeXmlComments(filePath);
}

使用摘要、备注和响应标签注释您的操作

/// <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(typeof(void), 500)]
public Product GetById(int id)

重建您的项目以更新 XML 注释文件并导航到 Swagger JSON 端点。注意描述是如何映射到 相应的 Swagger 字段。

【讨论】:

猜你喜欢
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-19
相关资源
最近更新 更多