.NET 拓展Swagger之【版本控制】

上一节

.NET 拓展Swagger之【添加注释】 - 掘金 (juejin.cn)

创建版本枚举

arduino 复制代码
namespace ZhaoXi.WebSiteApi.Utility.SwaggerExt
{
    public enum ApiVersion
    {
        V1,
        V2
    }
}

服务注册

erlang 复制代码
typeof(ApiVersion).GetEnumNames().ToList().ForEach(version =>
    {
        option.SwaggerDoc(version, new OpenApiInfo()
        {
            Title = $"标题",
            Version = version,
            Description = $"版本号为{version}"
        });
    });
csharp 复制代码
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    // app.UseSwaggerUI();

    app.UseSwaggerUI(c =>
    {
        foreach (string version in typeof(ApiVersion).GetEnumNames())
        {
            c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"第【{version}】版本");
        }
    });
}

测试

为方法绑定版本

在类上添加:[ApiExplorerSettings(IgnoreApi = false,GroupName = nameof(Utility.SwaggerExt.ApiVersion.V1))]

测试

方法封装

将原先的方法抽出,封装到CustomSwaggerExt类中

csharp 复制代码
using Microsoft.OpenApi.Models;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;

namespace ZhaoXi.WebSiteApi.Utility.SwaggerExt
{
    public static class CustomSwaggerExt
    {
        public static void AddSwaggerExt(this IServiceCollection services)
        {
            #region Swagger的配置

            services.AddEndpointsApiExplorer();
            services.AddSwaggerGen(option =>
            {
                // xml 文档文件绝对路径--读取根据控制器生成的xml文件
                var file = Path.Combine(AppContext.BaseDirectory,
                    "ZhaoXi.WebSiteApi.xml");
                // true: 显示控制器层注释
                option.IncludeXmlComments(file, true);
                // 对actuin的名称进行排序,如果有多个,就可以看到效果
                option.OrderActionsBy(o => o.RelativePath);

                typeof(ApiVersion).GetEnumNames().ToList().ForEach(version =>
                {
                    option.SwaggerDoc(version, new OpenApiInfo()
                    {
                        Title = $"标题",
                        Version = version,
                        Description = $"版本号为{version}"
                    });
                });

            });
            #endregion
        }


        public static void UseSwaggerExt(this WebApplication app)
        {
           
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    foreach (string version in typeof(ApiVersion).GetEnumNames())
                    {
                        c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"第【{version}】版本");
                    }
                });
            }
          
        }
    }
}

替换Program.cs代码

相关推荐
SelectDB3 分钟前
Apache Doris 中的 Data Trait:性能提速 2 倍的秘密武器
数据库·后端·apache
zhengzizhe12 分钟前
LangGraph4j LangChain4j JAVA 多Agent编排详解
java·后端
程序员鱼皮17 分钟前
又被 Cursor 烧了 1 万块,我麻了。。。
前端·后端·ai·程序员·大模型·编程
福大大架构师每日一题26 分钟前
2025-11-27:为视频标题生成标签。用go语言,给定一个字符串 caption(视频标题),按下面顺序处理并输出一个标签: 1. 将标题中的各个词合并成一
后端
程序员爱钓鱼27 分钟前
Go语言 OCR 常用识别库与实战指南
后端·go·trae
tonydf33 分钟前
动态表单之后:如何构建一个PDF 打印引擎?
后端
allbs35 分钟前
spring boot项目excel导出功能封装——4.导入
spring boot·后端·excel
用户693717500138443 分钟前
11.Kotlin 类:继承控制的关键 ——final 与 open 修饰符
android·后端·kotlin
用户69371750013841 小时前
10.Kotlin 类:延迟初始化:lateinit 与 by lazy 的对决
android·后端·kotlin
稚辉君.MCA_P8_Java1 小时前
通义 Go 语言实现的插入排序(Insertion Sort)
数据结构·后端·算法·架构·golang