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

ASP.NET Core 8.0 Web API未按预期绑定到localhost问题求助

.NET 8 API监听地址变为http://[::]:5678的问题解决

什么是[::]

[::]是IPv6的通配符地址,代表监听所有可用的IPv6网络接口,同时兼容IPv4双栈访问,作用等同于IPv4的0.0.0.0,并非localhost的简写。

地址变化的原因

你之前通过Visual Studio启动时,launchSettings.json里的FeatureFlagsAPI配置明确指定了applicationUrl: http://localhost:5678,且环境为Development。但直接运行API可执行文件时,不会读取launchSettings的配置,Kestrel会采用默认规则监听所有接口(包括IPv6),同时环境默认切换为Production,这就是地址显示变化的核心原因。

解决办法

方法1:命令行指定地址

运行API可执行文件时附加参数:

你的API文件名.exe --urls "http://localhost:5678"

方法2:通过appsettings.json配置Kestrel

修改appsettings.json,添加Kestrel节点指定绑定地址:

{
  "DetailedErrors": true,
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ApiUrl": "http://localhost:5678/",
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5678"
      }
    }
  }
}

方法3:在Program.cs中硬编码地址

将app.Run();修改为指定地址启动:

app.Run("http://localhost:5678");

方法4:通过环境变量指定

设置环境变量ASPNETCORE_URLS为http://localhost:5678,再运行API可执行文件。

额外提示

  • 该问题和CORS无关,CORS是跨域访问控制规则,与服务监听地址没有直接关联。
  • 如果想沿用launchSettings的配置启动,建议使用dotnet run命令,而非直接运行生成的exe文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 20:33:24