多專案(multiple projects)時,會產生多個 xml document, 叫用 IncludeXmlComments 時記得要把每個專案的 xml document 都載入,才能顯示不同專案中的註解和說明 (comment)。
範例如下:
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
//多個 Project, 每一個 xmldocument 都要載入
List<string> xmlFiles = Directory.GetFiles(AppContext.BaseDirectory, "*.xml", SearchOption.TopDirectoryOnly).ToList();
foreach (string fileName in xmlFiles)
{
string xmlFilePath = Path.Combine(AppContext.BaseDirectory, fileName);
if (File.Exists(xmlFilePath))
options.IncludeXmlComments(xmlFilePath, includeControllerXmlComments: true);
}
//防止 SchemaId 的錯誤
options.CustomSchemaIds(type => type.ToString());
});
這些都可以從註解中讀取: