二级标题
- nuGet 添加 Swagge
2.项目目录 右键-》属性-》生成-》输出 勾选xml 文档
3.Program.cs 文件中配置
var ApiName = "XXX.API";
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Version = "V1",
Title = $"{ApiName} 接口文档——Netcore 8.0",
Description = $"{ApiName} HTTP API V1",
});
c.OrderActionsBy(o => o.RelativePath);
// 为 Swagger JSON and UI设置xml文档注释路径
var basePath = Path.GetDirectoryName(AppContext.BaseDirectory);
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(basePath, xmlFile);
c.IncludeXmlComments(xmlPath);
});
// 数据库依赖注入
builder.Services.AddDbContext<AppDbContext>(options =>
{
options.UseSqlServer(builder.Configuration["DbContext:ConnectionString"]);
});
// 实现依赖注入
builder.Services.AddTransient<ITouristRouteRepository, TouristRouteRepository>();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"/swagger/v1/swagger.json", "WebApi.Core V1");
//路径配置,设置为空,表示直接在根域名(localhost:8001)访问该文件,注意localhost:8001/swagger是访问不到的,去launchSettings.json把launchUrl去掉,如果你想换一个路径,直接写名字即可,比如直接写
c.RoutePrefix = "doc";
c.RoutePrefix = string.Empty;
});
数据配置
1.appsettings.json
"DbContext": {
"ConnectionString": "server=数据库地址;Database=数据库名;User Id=用户名;Password=密码;Encrypt=False;"
},
"Authentication": {
"SecretKey": "suibianzifuchaun",
"Issuer": "xxx.com",
"Audience": "xxx.com"
}