Sli*_*icc 19 c# swagger swashbuckle
我正在使用SwaggerResponse属性来装饰我的api控制器动作,这一切都运行正常,但是当我查看生成的文档时,参数的描述字段为空.
是否有基于属性的方法来描述操作参数(而不是XML注释)?
Jul*_*iën 27
使用最新的Swashbuckle,或者更好地说至少我正在使用的Swashbuckle.AspNetCore变体,参数的Description字段现在可以正确显示为输出.
它确实需要满足以下条件:
[Http...]<param ...>xml注释描述参数完整示例如下所示:
/// <summary>
/// Short, descriptive title of the operation
/// </summary>
/// <remarks>
/// More elaborate description
/// </remarks>
/// <param name="id">Here is the description for ID.</param>
[ProducesResponseType(typeof(Bar), (int)HttpStatusCode.OK)]
[HttpGet, Route("{id}", Name = "GetFoo")]
public async Task<IActionResult> Foo([FromRoute] long id)
{
var response = new Bar();
return Ok(response);
}
Run Code Online (Sandbox Code Playgroud)
其中产生以下输出:
您应该确认您允许Swagger使用XML注释
httpConfig.EnableSwagger(c => {
if (GetXmlCommentsPath() != null) {
c.IncludeXmlComments(GetXmlCommentsPath());
}
...
...
);
protected static string GetXmlCommentsPath() {
var path = HostingEnvironment.MapPath("path to your xml doc file");
return path;
}
Run Code Online (Sandbox Code Playgroud)
您还应该检查是否正在为所需项目生成XML文档。在所需的项目属性下(Alt + Enter在项目顶部或右键单击->属性)-> 构建 ->检查XML文档文件