[英]Swagger API not refreshing the documentation
為了讓Swashbuckle從您的XML注釋中讀取,您需要為目標項目啟用XML文檔文件。 除此之外,您還需要在啟動配置中將Swashbuckle指向該文件。
來自Swashbuckle文檔 :
打開項目的“屬性”對話框,單擊“構建”選項卡,確保選中“XML文檔文件”。 這將生成一個包含構建時所有XML注釋的文件。
此時,任何未使用XML注釋注釋的類或方法都將觸發構建警告。 要禁止此操作,請在屬性對話框的“Supress warnings”字段中輸入警告代碼“1591”。*
配置Swashbuckle以將文件中的XML注釋合並到生成的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字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.