为什么我在运行 ASP.NET Core 示例项目时在我的 URL 中看到 [::]?

时间:2021-06-12 02:35:41

标签: asp.net-core ssl ipv4

我正在尝试使用 ASP.NET Core 和 SSL 证书,但在我几乎开始之前遇到了一些我不明白的事情。

我在 Linux Mint 20.1(主要是 Ubuntu)中运行 VS Code 和 net5.0。

我从 Web API 模板开始:

dotnet new webapi

它安装并运行没有问题,在浏览器中加载 https://localhost:5001/,这给出了 404,但 https://localhost:5001/swagger/index.htmlhttps://localhost:5001/WeatherForecast 都加载并运行没有问题。

然后我编辑了 launch.json 以将 uriFormat 添加到 serverReadyAction,因此它会在每次运行时加载 /WeatherForecast

"serverReadyAction": {
    "action": "openExternally",
    "pattern": "\\bNow listening on:\\s+(https?://\\S+)",
    "uriFormat": "%s/WeatherForecast"
},

当然,我在之前的实验中安装了 dotnet dev-cert。所以我清除了开发证书并再次运行:

dotnet dev-certs https --clean

然后当我尝试运行时,我得到了预期的错误:

<块引用>

无法配置 HTTPS 端点。未指定服务器证书,默认开发者证书找不到或已过期。

没有证书,就没有 https。

下一步是编辑 Properties/launchSettings.json,并删除 https 网址:

"applicationUrl": "http://localhost:5000",

这是由 HttpsRedirectionMiddleware 投诉造成的:

<块引用>

无法确定重定向的 https 端口。

但它加载了 http 页面没有问题。

那么让我们尝试一些替代方案:

"applicationUrl": "http://127.0.0.1:5000"

工作正常。

Mint 派生自 Ubuntu,Ubuntu 派生自 Debian,而 Debian 将主机名(在我的情况下为 Linux-2020)放在 /etc/host 中,分配给 127.0.1.1(这也是一个 IP4 环回地址)

127.0.0.1 localhost
127.0.1.1 Linux-2020

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

所以让我们再试试:

"applicationUrl": "http://Linux-2020:5000"

这样,VS Code 就弹出一个对话框——“你想让 Code 打开外部网站吗?”当我点击“打开”时,它会显示一个带有错误 URL 的页面:

http://[::]:5000/WeatherForecast

只是为了看看,我用 IP 试了一下:

"applicationUrl": "http://127.0.1.1:5000"

这也弹出了对话框,但是当我点击“打开”时,它打开了没有问题的页面:

http://127.0.1.1:5000/WeatherForecast

我已经用我在 /etc/hosts 中指定的其他域名尝试了这个,我使用什么名称并不重要,我使用哪个 IP4 环回地址也不重要 - ASP.NET Core 总是尝试打开 http:/[::]

我已将我机器的实际 IP 地址 (192.168.1.122) 的名称添加到 /etc/hosts

129.168.1.122 mine

当我使用我的实际 IP 时:

applicationUrl": "http://192.168.1.122:5000"

它有效。当我使用名称时:

applicationUrl": "http://mine:5000"

没有。

这是怎么回事?

0 个答案:

没有答案
相关问题