配置 Swagger + 注释

打开Xml文件生成,右键项目进入属性设置:

 取消未进行注释而进行提示的警告:

 然后Program.cs增加代码:

 代码:

builder.Services.AddSwaggerGen(s =>
{
    //获取xml文件名称
    var xmlFileName = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    //包含注释,第二个参数表示是否显示控制器注释
    s.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFileName), true);
    //添加安全定义
    s.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        Description = "请输入token,格式为 Bearer xxxxxxxx(注意中间必须有空格)",
        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[]{ }
        }
    });
});

 

posted @ 2024-07-01 09:39  木子zzgxl  阅读(32)  评论(0编辑  收藏  举报