1、新建 ApiVersionInfo.cs 版本文件;
/// <summary> /// API版本 /// </summary> public class ApiVersionInfo { public static string 接口版本1; }
右键编辑项目文件;
<PropertyGroup> <GenerateDocumentationFile>true</GenerateDocumentationFile> </PropertyGroup>
右键属性,在输出勾选生成api文档的文件;
2、Program.cs
var builder = WebApplication.CreateBuilder(args); builder.Services.AddSwaggerGen(options => { foreach (FieldInfo fileld in typeof(SeatSyatem.ApiVersionInfo).GetFields()) { options.SwaggerDoc(fileld.Name, new OpenApiInfo { Version = fileld.Name, Title = "API信息表", Description = $"API描述,{fileld.Name}版本" }); } var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; //IncludeXmlComments 第二参数 true 则显示 控制器 注释 options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename), true); }); var app = builder.Build(); app.UseSwagger(); //生成对应 swagger.json app.UseSwaggerUI(options => { foreach (FieldInfo field in typeof(SeatSyatem.ApiVersionInfo).GetFields()) { options.SwaggerEndpoint($"/swagger/{field.Name}/swagger.json", $"{field.Name}"); } });
3、在接口文件中写注释
/// <summary> /// 信息读写接口 /// </summary> [ApiController] [Route("[controller]")] [ApiExplorerSettings(GroupName = nameof(ApiVersionInfo.接口版本1))] public class RData : ControllerBase { /// <summary> /// 保存信息 /// </summary> /// <returns>返回code</returns> [HttpPost] public string Post() { return ""; }
标签:webapi,Name,ApiVersionInfo,public,版本,net6,swagger,options From: https://www.cnblogs.com/yingshiyu/p/17970711