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

如何在Visual Studio中为本地ASP.NET Core API指定特定URL?

解决方案

一、同一端口不同路径(https://localhost:1234/first/ & https://localhost:1234/second/)

单个端口只能被一个进程占用,直接让两个API共用同一端口行不通,得靠反向代理中转。用ASP.NET Core自带的YARP组件是最简单的实现方式,步骤如下:

  1. 新建一个空的ASP.NET Core项目作为代理网关
  2. 安装YARP包:Install-Package Microsoft.ReverseProxy
  3. 在网关项目的Program.cs中添加代理配置:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddReverseProxy()
    .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

var app = builder.Build();
app.MapReverseProxy();
app.Run();
  1. 在网关的appsettings.json中配置路由规则,将不同路径转发到对应API:
{
  "ReverseProxy": {
    "Routes": {
      "first-api-route": {
        "ClusterId": "first-api-cluster",
        "Match": {
          "Path": "/first/{**catch-all}"
        },
        "Transforms": [
          { "PathRemovePrefix": "/first" }
        ]
      },
      "second-api-route": {
        "ClusterId": "second-api-cluster",
        "Match": {
          "Path": "/second/{**catch-all}"
        },
        "Transforms": [
          { "PathRemovePrefix": "/second" }
        ]
      }
    },
    "Clusters": {
      "first-api-cluster": {
        "Destinations": {
          "first-api": {
            "Address": "https://localhost:5001/" // 填写第一个API的固定调试端口
          }
        }
      },
      "second-api-cluster": {
        "Destinations": {
          "second-api": {
            "Address": "https://localhost:5002/" // 填写第二个API的固定调试端口
          }
        }
      }
    }
  }
}
  1. 给两个API固定调试端口,修改各自的launchSettings.json:
    比如第一个API的配置:
"profiles": {
  "FirstAPI": {
    "commandName": "Project",
    "dotnetRunMessages": true,
    "launchBrowser": false,
    "applicationUrl": "https://localhost:5001;http://localhost:5000",
    "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "Development"
    }
  }
}

第二个API只需将端口改为https://localhost:5002;http://localhost:5003即可
6. 调试时先启动两个API项目,再启动网关项目(网关的端口也可在其launchSettings.json中固定为1234),此时访问https://localhost:1234/first/会转发到第一个API,https://localhost:1234/second/转发到第二个API

二、不同端口特定路径(https://localhost:1111/first/ & https://localhost:2222/second/)

这个场景完全可以通过修改launchSettings.json配合代码配置实现,操作简单:

  1. 给第一个API添加路由前缀,修改Program.cs:
    如果是控制器API:
var app = builder.Build();
// 其他中间件配置完成后添加以下代码
app.MapControllers().WithRoutePrefix("first");

如果是最小API:

app.MapGroup("/first").MapControllers();
  1. 固定第一个API的端口,修改它的launchSettings.json:
"profiles": {
  "FirstAPI": {
    "commandName": "Project",
    "dotnetRunMessages": true,
    "launchBrowser": false,
    "applicationUrl": "https://localhost:1111;http://localhost:1110",
    "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "Development"
    }
  }
}
  1. 第二个API重复上述操作:将路由前缀设为second,端口固定为2222
  2. 直接启动两个API,即可通过指定地址访问

关于launchSettings.json的说明

  • 不同端口的场景,完全可以通过修改launchSettings.json配合代码中的路由前缀实现
  • 同一端口的场景,仅靠launchSettings.json无法完成,必须借助反向代理——因为单个端口只能被一个进程占用,需要网关来转发请求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 17:30:14