.net core 3.1 swagger文档
程序员文章站
2024-01-21 20:56:46
...
.net core 3.1 swagger文档
1.安装包
Swashbuckle.AspNetCore
2.在Startup.cs 配置swagger 在ConfigureServices中添加代码
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
//Core.Admin.webapi.xml是我的项目生成XML文档的后缀名,具体的以你项目为主
var xmlPath = Path.Combine(basePath, "test.API.xml");
c.IncludeXmlComments(xmlPath);
});
//分割线
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
3.打开主项目属性——》生成 打开xml 并且加上 1591(不加报警告 很烦)
4.访问 swagger/index.html
5.启用jwt验证 添加heads 请求头
//接口文档
services.AddSwaggerGen(s =>
{
s.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
//Core.Admin.webapi.xml是我的项目生成XML文档的后缀名,具体的以你项目为主
var xmlPath = Path.Combine(basePath, "API.xml");
s.IncludeXmlComments(xmlPath);
s.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
Description = "在下框中输入请求头中需要添加Jwt授权Token:Bearer Token",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
BearerFormat = "JWT",
Scheme = "Bearer"
});
s.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme{
Reference = new OpenApiReference {
Type = ReferenceType.SecurityScheme,
Id = "Bearer"}
},new string[] { }
}
});
});
//分割线
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
上一篇: 活用LINUX时间函数
推荐阅读
-
.net core 3.1 swagger文档
-
.net core 安装Swagger
-
.net core swagger安装
-
发布.net core3.0 webapi +swagger
-
Asp.Net Core WebApi中接入Swagger组件(初级)
-
分享一个集成.NET Core+Swagger+Consul+Polly+Ocelot+IdentityServer4+Exceptionless+Apollo的入门级微服务开发框架
-
Asp.net Core 3.1基于AspectCore实现AOP实现事务、缓存拦截器功能
-
.Net Core 分布式微服务框架 - Jimu 添加 Swagger 支持
-
使用Swagger服务搭建.Net Core API
-
asp.net core 3.0 中使用 swagger