【发布时间】:2022-01-20 16:10:25
【问题描述】:
Swagger xml cmets 没有显示在文档 UI 中,不确定我在这里遗漏了什么.. 至少有人指示我这是一个错误
第一步:新建一个全新的 ASP.NET Web 应用程序 Web API 项目
第 2 步:创建 Web API 项目
第三步:安装 Swashbuckle 5.6.0 NuGet 包
Step4:启用生成 XML 文档文件(项目属性 -> 构建)
第 5 步:更新 SwaggerConfig.cs 以包含 XmlComments
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration.EnableSwagger(c =>
{
var xmlFile = "bin\\" + $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
}
第六步:向控制器添加 XML cmets
///<Summary>
/// Get these comments1
///</Summary>
public class ValuesController : ApiController
{
///<Summary>
/// Get these comments2
///</Summary>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
WebApplication1.xml也在bin文件夹中生成
<?xml version="1.0"?>
<doc>
<assembly>
<name>WebApplication1</name>
</assembly>
<members>
<member name="T:WebApplication1.Controllers.ValuesController">
<Summary>
Get these comments1
</Summary>
</member>
<member name="M:WebApplication1.Controllers.ValuesController.Get">
<Summary>
Get these comments2
</Summary>
</member>
<member name="M:WebApplication1.Controllers.ValuesController.Get(System.Int32)">
<Summary>
Get these comments3
</Summary>
</member>
<member name="M:WebApplication1.Controllers.ValuesController.Post(System.String)">
<Summary>
Get these comments4
</Summary>
</member>
<member name="M:WebApplication1.Controllers.ValuesController.Put(System.Int32,System.String)">
<Summary>
Get these comments5
</Summary>
</member>
<member name="M:WebApplication1.Controllers.ValuesController.Delete(System.Int32)">
<Summary>
Get these comments6
</Summary>
</member>
</members>
</doc>
【问题讨论】:
-
在xmlPath变量处下断点,检查路径是否正确
-
XML 路径是正确的,我确实看到了在 bin 文件夹中创建的带有 cmets 的 XML 文件
-
第一步是创建 ASP.NET Core 项目,但在将 Swashbuckle 包添加到 ASP.NET Web API 时会创建 SwaggerConfig.cs。如果您使用的是 Core,请使用 Swashbuckle.AspNetCore 包
-
@YegorAndrosov 你找到解决方案了吗?
-
@GuilhermeWaltricke OP 的问题是因为他们使用的是 ASP.NET 包而不是 Core
标签: asp.net-core swagger swagger-2.0