.netcore2.2使用Swagger

1.swagger的引用

引用Swashbuckle.AspNetCore和Swashbuckle.AspNetCore.Swagger

2.项目属性更改。勾选xml文档文件

.netcore2.2使用Swagger

3.在ConfigureServices中注册

#region swagger
        //注册Swagger生成器,定义一个和多个Swagger 文档
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info
            {
                Version = "v1",         //版本
                Title = "hl's API",     //标题
                Description = "API描述",//描述
                TermsOfService = "None",
                Contact = new Contact   //联系人信息
                {
                    Name = "**",            
                    Email = string.Empty,
                }
            });
            // 为 Swagger JSON and UI设置xml文档注释路径
            var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
            var xmlPath = Path.Combine(basePath, "Test.xml");//xml路径名称
            c.IncludeXmlComments(xmlPath);
        });
        #endregion

4.在Configure中添加中间件

//启用中间件服务生成Swagger作为JSON终结点
app.UseSwagger();
app.UseSwaggerUI(c =>
{
     c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
     c.RoutePrefix = string.Empty;
});

5.项目启动
.netcore2.2使用Swagger

相关推荐