net core swagger显示注释

ConfigureServices中配置

  services.AddSwaggerGen(options =>
            {
                //var xmlPath = Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml");
                options.SwaggerDoc("v1", new OpenApiInfo { Title = "Api", Version = "v1", Description = "API", });
                var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径)
                var xmlPath = Path.Combine(basePath, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml");//接口action显示注释
                options.IncludeXmlComments(Path.Combine(basePath, "YiSha.Entity.xml"), true);//分层实体显示注释
                options.IncludeXmlComments(Path.Combine(basePath, "YiSha.Model.xml"), true);//分层实体显示注释
            });

注意要生成的xml的项目,不然报这个文件不存在,需要的项目都生成这个

 

Configure中配置

  app.UseSwagger(c =>
            {
                c.RouteTemplate = "api-doc/{documentName}/swagger.json";
            });
            app.UseSwaggerUI(c =>
            {
                c.RoutePrefix = "api-doc";
                c.SwaggerEndpoint("v1/swagger.json", "v1");
            });

生成效果是都有注释

 

posted @ 2022-01-26 16:32  世人皆萌  阅读(301)  评论(0编辑  收藏  举报