.net core api swagger添加类注释
1、右键类库属性

2、添加Swagger扩展
public static class SwaggerGenOptionsExtensions { public static void IncludeXmlComments(this SwaggerGenOptions swaggerGenOptions, string location) { // 解决相同类名会报错的问题 swaggerGenOptions.CustomSchemaIds(type => type.FullName); foreach (var item in Directory.GetFiles(Path.GetDirectoryName(location), "*.xml")) { swaggerGenOptions.IncludeXmlComments(item, true); } } }
3、使用
builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new() { Title = "Messhall.HostService", Version = "v1" }); // 解决相同类名会报错的问题 c.IncludeXmlComments(Assembly.GetEntryAssembly()?.Location); });
浙公网安备 33010602011771号