IIS Express,ASP.NET Core - 无效的 URI:无法解析主机名

IIS Express, ASP.NET Core - Invalid URI: The hostname could not be parsed

周末回来调试我的 Web 项目,它是一个 ASP.NET Core Web Api。它开始给我一个错误:无效的 URI:无法解析主机名。

我可以启动一个新的 asp.net 核心 Web api 项目,它调试得很好,所以我很确定它与我对该项目的配置有关。有什么想法吗?

詹姆斯

解决方案:

  1. 关闭Visual Studio
  2. 删除 .vs 文件夹

固定:)

Jakub 的回答确实解决了这个问题,因为它导致 Visual Studio 重新生成 applicationhost.config。在我的例子中,错误是说无法解析 <binding> 标签的值。我有以下内容: <binding protocol="http" bindingInformation="*:5000:*" /> 再生时看起来像这样: <binding protocol="http" bindingInformation="*:5000:localhost" /> 该文件位于“.vs”文件夹的 "config" 子文件夹中,而不是删除整个目录,您可以 fix/restore 您的 <binding> 标记为可以解析的值。

编辑:根据* 不是有效的主机名。如果您希望它绑定到所有主机名,只需将主机留空即可。例如:*:80: 而不是 *:80:* 表示 "All network interfaces/IPs, on port 80, for all hostnames".

此错误表明您的网站绑定信息格式不正确。

bindingInformation 的值由三部分组成。

ip:port:host

以下是绑定的一些格式及其结果:

<!-- Configures the site with a hostname of "www.test.com" on port 80 for the IP address of 192.168.1.10. -->
<binding protocol="http" bindingInformation="192.168.1.10:80:www.test.com" />

<!-- Configures the site with a hostname of "localhost" on port 80 for the IP address of 192.168.1.10. -->
<binding protocol="http" bindingInformation="192.168.1.10:80:localhost" />

<!-- Configures the site without a hostname and IP address on port 80. You'd use this setting to make your site directly accessible via any address that the system has, including the localhost address at 127.0.0.1, as well as any and all configured IP addresses. -->
<binding protocol="http" bindingInformation=":80:" />

<binding protocol="https" bindingInformation="*:443:" />
<!-- Configures HTTPS bindings for all IP addresses over port 443. -->

对于上述所有绑定,您可以将协议设置为 https,使您的站点只能通过 SSL 访问。

我post这个回答是因为很多人觉得我的评论有用。感谢@joshcomley 戳我。

希望这个回答能对某人有所帮助:

  1. 在您的解决方案文件夹中找到名为 .vs 的子文件夹(注意,此文件夹具有隐藏属性,因此文件资源管理器默认情况下不会显示它)
  2. 打开 .vs/config/applicationhost.config 文件
  3. 在文件中找到类似 <site name="<Your_Web_Site_Name>" .... 的元素。你可以有几个这样的元素。您需要选择一个 <Your_Web_Site_Name> 与您的站点名称相匹配的
  4. <site 元素中找到一个子元素,例如:

<binding protocol="http" bindingInformation=":8080:localhost" />

并将其替换为:

<binding protocol="http" bindingInformation=":8080:" />

  1. 重建解决方案和运行网站

备注:

  • 以端口号8080为例。您应该分配网站实际使用的端口。
  • 此修复适用于 IISExpress 中托管的网站。它还允许避免像 Invalid URI: The hostname could not be parsed.
  • 这样的错误消息
  • 如果您的网站正在使用 IIS,您可以尝试使用以下行替换绑定: <binding protocol="http" bindingInformation="*:8080:*" />。并在此更改后执行 iisreset

我遇到了类似的问题,运行管理员模式下的 Visual Studio 为我解决了这个问题。

我遇到了同样的问题并尝试了 post 中的其他建议,但没有奏效。所以我去了项目的属性 - >调试 - > WebServer设置 在那里我用 localhost 替换了 * 并为我解决了这个问题。 Properties capture