You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在主项目中统一启动多API并共用单一端口?

实现方案(基于ASP.NET Core + YARP反向代理)

1. 给各API项目配置固定内部端口

打开每个TestX.API项目的launchsettings.json,修改applicationUrl为固定内部端口(避免随机端口导致代理失效),示例:

{
  "profiles": {
    "Test1.API": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5001",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

依次给Test2.API设为http://localhost:5002,Test3.API设为http://localhost:5003,以此类推配置剩余API项目。

2. 主项目集成YARP反向代理

2.1 安装YARP包

在MainTestAPI项目中通过NuGet包管理器安装Microsoft.ReverseProxy,或使用命令行:

dotnet add package Microsoft.ReverseProxy

2.2 配置代理路由

.NET 6+(Program.cs)

var builder = WebApplication.CreateBuilder(args);

// 注册YARP服务并加载配置
builder.Services.AddReverseProxy()
    .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

var app = builder.Build();

// 启用代理路由
app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapReverseProxy();
});

app.Run();

在appsettings.json中添加代理规则:

{
  "ReverseProxy": {
    "Routes": {
      "test1_route": {
        "ClusterId": "test1_cluster",
        "Match": {
          "Path": "/api/test1/{**catch-all}"
        }
      },
      "test2_route": {
        "ClusterId": "test2_cluster",
        "Match": {
          "Path": "/api/test2/{**catch-all}"
        }
      },
      "test3_route": {
        "ClusterId": "test3_cluster",
        "Match": {
          "Path": "/api/test3/{**catch-all}"
        }
      }
      // 补充test4、test5的路由配置,格式与上述一致
    },
    "Clusters": {
      "test1_cluster": {
        "Destinations": {
          "test1_destination": {
            "Address": "http://localhost:5001/"
          }
        }
      },
      "test2_cluster": {
        "Destinations": {
          "test2_destination": {
            "Address": "http://localhost:5002/"
          }
        }
      },
      "test3_cluster": {
        "Destinations": {
          "test3_destination": {
            "Address": "http://localhost:5003/"
          }
        }
      }
      // 补充test4、test5的集群配置,格式与上述一致
    }
  }
}

.NET 5及以前(Startup.cs)

在ConfigureServices中注册服务:

services.AddReverseProxy()
    .LoadFromConfiguration(Configuration.GetSection("ReverseProxy"));

在Configure中启用代理:

app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapReverseProxy();
});

3. 配置各API的Swagger适配代理路径

每个TestX.API项目需修改Swagger配置,确保UI和接口请求适配代理路径:

3.1 修改Swagger路由前缀

以Test1.API为例,在Program.cs(或Startup.cs的Configure方法)中:

// .NET 6+示例
app.UseSwagger(options =>
{
    options.RouteTemplate = "api/test1/swagger/{documentname}/swagger.json";
});
app.UseSwaggerUI(options =>
{
    options.SwaggerEndpoint("/api/test1/swagger/v1/swagger.json", "Test1.API V1");
    options.RoutePrefix = "api/test1/swagger";
});

将代码中的test1替换为对应API的名称(test2、test3等)。

3.2 配置Swagger Server地址

在Swagger生成逻辑中添加Server配置,确保接口请求指向代理后的地址:

// .NET 6+示例,在AddSwaggerGen中添加
builder.Services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new OpenApiInfo { Title = "Test1.API", Version = "v1" });
    options.AddServer(new OpenApiServer { Url = "/api/test1" });
});

同样将test1替换为对应API的名称。

4. 设置解决方案同时启动所有项目

在Visual Studio中操作:

  • 右键解决方案 → 属性 → 启动项目
  • 选择多个启动项目,将MainTestAPI和所有TestX.API的操作均设为启动
  • 可选择并行启动(默认)或调整启动顺序

启动主项目后,所有API项目会一同启动,主项目通过代理将/api/testX路径的请求转发到对应API的内部端口,实现单端口访问所有API的需求。

内容的提问来源于stack exchange,提问作者Sami In

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 21:40:09