.netcore2.2 use Swagger

1.swagger references

引用Swashbuckle.AspNetCore和Swashbuckle.AspNetCore.Swagger

2. Change the project properties. Check xml document file

3. Registration in the 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. Add the middleware Configure

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

The project started

Guess you like

Origin www.cnblogs.com/papermask/p/12092874.html