当前位置: 首页 > news >正文

SwaggerEndPoints 配置访问外部 Swagger 文档

在 appsettings.json 中添加 Swagger 端点配置

{"Logging": {"LogLevel": {"Default": "Information","Microsoft.AspNetCore": "Warning"}},"SwaggerEndPoints": [{"Key": "plant","Config": [{"Name": "CentData API","Version": "v1","Url": "http://127.16.34.22:55002/swagger/v1/swagger.json" // 模拟的外部 Swagger URL}]},{"Key": "Alarm","Config": [{"Name": "AlarmData API","Version": "v1","Url": "http://127.16.34.22:55004/swagger/v1/swagger.json" // 模拟的外部 Swagger URL}]}],"AllowedHosts": "*"
}

修改 Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System.Collections.Generic;public class Startup
{public Startup(IConfiguration configuration){Configuration = configuration;}public IConfiguration Configuration { get; }public void ConfigureServices(IServiceCollection services){services.AddControllers();services.AddSwaggerGen(c =>{c.SwaggerDoc("v1", new OpenApiInfo { Title = "API Documentation", Version = "v1" });// 从配置中读取 Swagger 端点var swaggerEndpoints = Configuration.GetSection("SwaggerEndPoints").Get<List<SwaggerEndpointConfig>>();foreach (var endpoint in swaggerEndpoints){foreach (var config in endpoint.Config){c.SwaggerDoc(endpoint.Key, new OpenApiInfo { Title = config.Name, Version = config.Version });}}});}public void Configure(IApplicationBuilder app, IWebHostEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseRouting();app.UseAuthorization();// 启用 Swagger 中间件app.UseSwagger();app.UseSwaggerUI(c =>{c.RoutePrefix = "swagger"; // Swagger UI 的前缀var swaggerEndpoints = Configuration.GetSection("SwaggerEndPoints").Get<List<SwaggerEndpointConfig>>();foreach (var endpoint in swaggerEndpoints){foreach (var config in endpoint.Config){// 使用 Url 属性来添加外部 Swagger 端点if (!string.IsNullOrWhiteSpace(config.Url)){c.SwaggerEndpoint(config.Url, $"{config.Name} v{config.Version}");}else{c.SwaggerEndpoint($"/swagger/{endpoint.Key}/swagger.json", $"{config.Name} v{config.Version}");}}}});app.UseEndpoints(endpoints =>{endpoints.MapControllers();});}
}// Swagger 端点配置类
public class SwaggerEndpointConfig
{public string Key { get; set; }public List<SwaggerConfig> Config { get; set; }
}public class SwaggerConfig
{public string Name { get; set; }public string Version { get; set; }public string Url { get; set; } // Optional: 如果您希望直接使用 URL
}

相关文章:

  • 使用蓝耘元生代 MaaS 平台 API 工作流调用技巧与实践体验
  • 九级融智台阶与五大要素协同的量子化解析
  • 仿盒马》app开发技术分享-- 确认订单页(数据展示)(端云一体)
  • 迪宇电力绝缘胶垫四大优势,用特殊橡胶配方制成,具备多项实用优势
  • Day31 -js应用 -实例:webpack jQuery的使用及其隐含的安全问题
  • MySQL 窗口函数深度解析:语法、应用场景与性能优化
  • 理解vue-cli 中进行构建优化
  • 不同电脑同一个网络ip地址一样吗?如何更改
  • HTML Day02
  • C++—decltype
  • 大模型(5)——编码器(Encoder)、解码器(Decoder)
  • 大模型微调(面经总结)
  • Linux编译器——gcc/g++的使用
  • 实时数仓和离线数仓的区别是什么?企业如何选择合适的数仓架构?
  • 华为云Flexus+DeepSeek征文|ModelArts Studio开通DeepSeek-V3与R1商用服务实践与体验
  • Playwright 常用命令、参数详解及使用示例
  • springboot调用redis数据库,操作字符串
  • Java基础打卡-集合2025.05.22
  • Oracle20200714GI_PSU补丁流程及问题收集
  • [yolov11改进系列]基于yolov11引入全维度动态卷积ODConv的python源码+训练源码
  • 秦皇岛市 网站建设/爱站站长工具
  • 江苏建设人才无纸化考核网站/app推广30元一单
  • 重庆cms建站模板/关键词优化平台有哪些
  • 虚拟主机可以做几个网站/今日热搜榜排名最新
  • 建站软件有哪些功能/世界杯排名
  • 嘉兴网站制作建设/seo引流什么意思