Sam*_*eer 7 c# asp.net-web-api swagger swashbuckle
我正在使用Swagger API来记录我的REST服务.之前我的控制器方法没有提供信息性的评论,因此Swagger API没有显示描述,但现在甚至在更新评论后,我没有在突出显示的区域中获得方法描述.
/// <summary>
/// Gets the consumer scores by retailer id and return id
/// </summary>
/// <param name="retailerId"></param>
/// <param name="returnId"></param>
/// <returns></returns>
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
mcl*_*129 10
为了让Swashbuckle从您的XML注释中读取,您需要为目标项目启用XML文档文件.除此之外,您还需要在启动配置中将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);
}
Run Code Online (Sandbox Code Playgroud)
使用摘要,备注和响应标记注释您的操作
/// <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)
Run Code Online (Sandbox Code Playgroud)
重建项目以更新XML注释文件并导航到Swagger JSON端点.请注意描述如何映射到相应的Swagger字段.
| 归档时间: |
|
| 查看次数: |
6135 次 |
| 最近记录: |