【问题标题】:Cannot hit ASP.NET Core API from remote computer on network无法从网络上的远程计算机访问 ASP.NET Core API
【发布时间】:2020-02-26 13:51:44
【问题描述】:

我通过dotnet new webapi (v3.1.101) 创建了示例dotnet webapi 项目,并将launchSettings.json 的applicationUrl 首先更新为http:*:5000,然后更新为http:0.0.0.0:5000。当我尝试转到http://<MY_COMPUTER_NETWORK_NAME>:5000/WeatherForecast 时,它会超时,但是当我在本地尝试http://localhost:5000/WeatherForecast 时,它可以工作。

我在端口 5000 上运行了一个 React 应用程序,并且我可以成功地远程连接到它,所以我认为这不是防火墙问题。

我在 Mac 上运行。我也尝试添加.UseUrls(...),但没有成功。

为什么我无法远程连接?

提前致谢!

【问题讨论】:

  • 你在使用防火墙吗?
  • 我有一个,但我通过 React App 验证端口 5000 已打开
  • 愚蠢的问题:您是否在修改启动设置后重新启动了应用程序,不是吗?
  • @Ian - 什么鬼?在调试模式下运行它可以工作!我想知道如何让它在终端中正确运行?
  • @CodeSlinger512 - 将评论更改为回答,因为它似乎是一个。还添加了描述如何使用终端的链接(我认为)。

标签: asp.net-web-api .net-core


【解决方案1】:

我在 Windows 中使用 VSCode,我注意到我无法从终端使用 dotnet run 命令,我必须从 Debug 侧边栏窗口点击播放才能工作,然后我可以从其他计算机连接同一个网络。

如果您想使用终端启动,那么我认为您必须在选项中手动指定您的 launch.json 文件。在此处查看 --launch-profile 选项:https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-run

【讨论】:

    【解决方案2】:

    dotnet run 将尝试自动加载MyApplication\Properties\launchSettings.json。 如果文件launchSettings.json 存在并且设置了“所有地址”绑定:

    {
      "profiles": {
        "Uno": {
          "commandName": "Project",
          "launchBrowser": true,
          "applicationUrl": "https://0.0.0.0:5001;http://0.0.0.0:5000",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    

    然后运行应用程序应该得到这个结果:

    > dotnet run
    info: Microsoft.Hosting.Lifetime[0]
        Now listening on: https://0.0.0.0:5001
    info: Microsoft.Hosting.Lifetime[0]
        Now listening on: http://0.0.0.0:5000
    

    并检查开放端口:

    > netstat -na | ? { $_ -match "5000|5001" }
      TCP    0.0.0.0:5000           0.0.0.0:0              LISTENING
      TCP    0.0.0.0:5001           0.0.0.0:0              LISTENING
    

    如果 launchSettings.json 不存在或存在不同的名称,将应用默认绑定:

    > mv .\Properties\launchSettings.json .\Properties\launch.json
    > dotnet run
    info: Microsoft.Hosting.Lifetime[0]
          Now listening on: http://localhost:5000
    info: Microsoft.Hosting.Lifetime[0]
          Now listening on: https://localhost:5001
    

    除非您使用特定选项指定网址: server.urls="http://0.0.0.0:5000;https://0.0.0.0:5001"

    另请注意,任何应用程序第一次尝试绑定到 0.0.0.0:port 时,会出现如下窗口: 如果您处于多显示器设置或立即关注其他窗口,则很容易错过它。此外,此对话框将设置特定于应用程序的防火墙规则,因此您可能为 React 应用程序创建了规则,而不是为 dotnet 应用程序创建了规则。

    【讨论】:

    • 感谢您的意见。我已将launchSettings.json 中设置的那些字段设置为0.0.0.0,并且输出看起来与您显示的一样。我也在 Mac 上运行,而不是 Windows。
    猜你喜欢
    • 2015-01-07
    • 2010-12-10
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 2015-08-05
    • 2021-10-20
    相关资源
    最近更新 更多