【问题标题】:Bad Request - Invalid Hostname when accessing localhost Web API or Web App from across LAN错误请求 - 从跨 LAN 访问本地主机 Web API 或 Web 应用程序时主机名无效
【发布时间】:2017-12-22 04:02:17
【问题描述】:

我有一个 ASP .Net Core 1.1 Web API 和 Web 应用程序在我的本地主机上从 Windows 10 上的 Visual Studio 2017 运行。

当我运行项目时,API 在http://localhost:50082/api/ 上运行,而网络应用在http://localhost:60856/ 上运行

但是,如果 LAN 上的其他人尝试访问它(使用我计算机的 IP 地址 - http://192.168.1.101:60856/ 他们会得到一个

错误的请求 - 无效的主机名

错误。实际上,。我也收到此错误,因为我使用了我的 IP 地址。我在 C:\Users\Documents\IISExpress\config\applicationhost.config 文件中尝试了十几种变体,例如:

<bindings>
<binding protocol="http" bindingInformation="*:60856:localhost" />
</bindings>

<bindings>
<binding protocol="http" bindingInformation="*:60856:" />
</bindings>

每次都重新启动项目(以及因此 IIS Express),但似乎没有任何效果。有什么想法吗?

【问题讨论】:

  • 我也关闭了防火墙,并且没有运行任何第三方杀毒软件...

标签: asp.net-core-mvc iis-express asp.net-core-1.1


【解决方案1】:

默认情况下,IIS Express 不允许远程连接。 This post 解释了如何激活它。

在我看来,最好设置本地 IIS 并将应用程序发布到配置的 IIS 文件夹。

我已经尝试过其他情况下的 HTTP.SYS 配置,有时无论是重启还是 Windows 更新,配置都会丢失...但这可能不会发生在您身上...

【讨论】:

【解决方案2】:

我在尝试让 ngrok 指向本地 ASP.NET Core 网站时遇到了同样的问题。

这个人的回答对我有用:https://kodlogs.com/blog/532/iis-express-http-error-400-the-request-hostname-is-invalid

基本上,在[solution dir]\.vs\[asp.net core web app dir]\config\applicationhost.config 文件中,将行从:

<binding protocol="https" bindingInformation="*:[your port]:localhost" />

到:

<binding protocol="https" bindingInformation="*:[your port]:*" />

【讨论】:

    【解决方案3】:

    如果您使用的是 asp 核心,另一种解决方案是从 Visual Studio 工具栏中将您的配置文件从“IIS”简单地更改为“独立”(命名为您的项目):

    通过此更改,IIS 将不再运行,并且嵌入式 asp Web 服务器“红隼” 将直接面对请求。

    如果它仍然给您主机无效错误,请通过将此行添加到您的配置文件来调整您的主机名设置:

    "AllowedHosts": "*",
    

    更多信息:

    https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/?view=aspnetcore-5.0#set-up-a-reverse-proxy

    https://weblog.west-wind.com/posts/2019/Mar/16/ASPNET-Core-Hosting-on-IIS-with-ASPNET-Core-22

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/host-filtering?view=aspnetcore-5.0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-10
      • 2017-09-17
      • 2015-08-17
      • 2017-08-18
      • 2017-06-17
      • 2016-10-20
      • 2021-10-09
      • 2016-03-26
      相关资源
      最近更新 更多