.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);
});

 

posted @ 2021-12-24 11:08  棙九九  阅读(352)  评论(0)    收藏  举报