错误请求 - 从 LAN 访问 localhost Web API 或 Web App 时主机名无效

Bad Request - Invalid Hostname when accessing localhost Web API or Web App from across LAN

我有一个 ASP .Net Core 1.1 Web API 和 Web App 运行ning 在我的本地主机上 Visual Studio 2017 Windows 10。

当我 运行 项目时, API 运行 在 http://localhost:50082/api/ and the web app on 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),但似乎没有任何效果。有什么想法吗?

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

在我看来,如果您设置本地 IIS 并将应用程序发布到配置的 IIS 文件夹中,您的情况会更好。

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

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

这个人的回答对我有用: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]:*" />

如果您使用的是 asp 内核,另一个解决方案是简单地将您的配置文件从 'IIS' 更改为 'Standalone'(以您的项目命名)从 Visual Studio工具栏:

通过此更改,IIS 将不再 运行 和嵌入式 asp 网络服务器 'kestrel' 将直接面对请求。

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

"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