当前位置:首页 > Web开发 > 正文

}});// 为 Swagger JSON and UI设置xml文档注释路径var basePath = Path.G

2024-03-31 Web开发

1.swagger的引用

引用Swashbuckle.AspNetCore和Swashbuckle.AspNetCore.Swagger

2.项目属性变动。勾选xml文档文件

技术图片

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

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/33146.html