namespace Microsoft.Extensions.DependencyInjection // SwaggerGenOptionsExtensions.cs
// .......
public static void IncludeXmlComments(
this SwaggerGenOptions swaggerGenOptions,
string filePath,
bool includeControllerXmlComments = false)
{
swaggerGenOptions.IncludeXmlComments((Func<XPathDocument>) (() => new XPathDocument(filePath)), includeControllerXmlComments);
}
// Program.cs
// ......
if (builder.Environment.IsDevelopment())
{
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1",
new OpenApiInfo { Title = "这是标题", Version = "v1", Description = "这是描述" });
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename), true);
options.OrderActionsBy(o => o.RelativePath);
});
}