使用IP地址而不是localhost时出现HTTP错误400

And*_*res 3 c# asp.net rest

我使用带有C#和VS2015的Rest API创建了一个简单的Web应用程序。该网络应用正在运行,http://localhost:1623并且运行正常。

现在,我正在测试Rest API,以进行POST Postman,例如,我的网址http://localhost:1623/api/foo/bar带有标头Content-Type: application/json。一切正常,我得到正确的答复。

我的PC的本地IP地址是192.168.0.174,所以我将URL更改为http://192.168.0.174:1623/api/foo/bar现在,我得到了HTTP Error 400. The request hostname is invalid,为什么?不应该一样吗?如果尝试使用192.168.0.174:1623而不是http://localhost:1623在浏览器中访问Web应用程序,则会发生相同的错误(错误完全相同):

在此处输入图片说明

当尝试使用IP通过LAN中的其他设备使用API​​时,出现相同的错误。我需要使用IP,因为我需要从网络访问它,为什么它不起作用,我还缺少什么?我尝试了这个没有成功。

And*_*res 5

通过答案编辑解决了问题,问题applicationhost.config在VS2015中位于[Project Folder]\.vs\config\applicationhost.config:

由于杰克答案的链接URL当前不起作用,并且解决方案(至少对我而言)已包含在另一个问题的答案中,因此我将在此处重复答案:

您可以设置多个绑定。因此,您可以为希望使用的每个外部地址设置绑定,它将起作用:

<bindings>
    <binding protocol="http" bindingInformation=":1904:" />
    <binding protocol="http" bindingInformation=":1904:machineName" />
    <binding protocol="http" bindingInformation=":1904:10.1.10.123" />
</bindings>
Run Code Online (Sandbox Code Playgroud)

同样是有益的。就我而言:

<bindings>
    <binding protocol="http" bindingInformation=":1623:" />
    <binding protocol="http" bindingInformation=":1623:<my pc name>" />
    <binding protocol="http" bindingInformation=":1623:192.168.0.174" />
</bindings>
Run Code Online (Sandbox Code Playgroud)