错误请求-Asp.netVisual Studio 2013中的www.example.com WebAPI项目的主机名无效

ecfsfe2w  于 2023-06-24  发布在  .NET
关注(0)|答案(6)|浏览(149)

我在Visual Studio Pro 2013中运行一个非常基本的webAPI项目。它在我的机器上的localhost上运行良好。然后,我尝试从另一台机器上转到浏览器,然后选择goto:57571,这类似于我如何通过将服务器的ip地址和端口号指向rails应用程序。然后我得到Bad Request - Invalid HostnameHTTP Error 400。请求的主机名无效。
我也复制了这一点,把两台windows机器放在azure中。一个运行Visual Studio Pro 2013。我运行相同的应用程序,它在本地工作正常。然后,如果我尝试从另一个VM指向它(在同一个虚拟网络上,我也可以ping服务器VM),我仍然得到Bad Request - Invalid Hostname HTTP Error 400。请求主机名无效。
请注意,我不关心一个自定义域名,我只是想能够运行这个应用程序从另一台机器使用的IP地址。这可能吗?(如果是这样,有什么想法我做错了吗?)这是我试过的Didnt work for me这也没有使它工作
我怎么知道visual studio是以管理员身份运行的?
(我使用的是Windows Server 2012 R2,其中运行的是Visual Studio。我关闭了两台机器上的防火墙)

oxf4rvwz

oxf4rvwz1#

下面是我如何让它工作的。
后藤C:\users\yourusername\Documents\IISExpress\config\applicationhost.configVisual Studio 2013
将下面这行添加到applicationhost.config中,使其工作

<binding protocol="http" bindingInformation="*:53676:*" />

这不起作用

<binding protocol="http" bindingInformation="<clientsIPaddress>:53676:*" />

Vs2015更新和此更改的确切位置****应用程序.config**如下所示。

VS 2015的文件路径如下所示。
C:\Users\\{YourUsername}\Documents\Visual Studio 2015\Projects\\{ThisSolutionName}\\.vs\config\applicationhost.config正如丹尼尔所说。

我应该把它放在我的application.config中的什么位置?

下面是XML文件中的父节,您可以在这些父节中找到绑定。

<system.applicationHost>
           <sites>
              <site name="WebSite1" id="1" serverAutoStart="true">
                 <bindings>
                      <binding protocol="http" bindingInformation=":8080:localhost" />
               </bindings>
           </site>
      </sites>
 </system.applicationHost>

这是大约第161行的新项目,在applicationhost.config中没有任何更改。
我没有意识到的是我认为客户端的IP地址是什么,实际上不是真的。原因是我在Azure中有一个VNet到VNet的连接,连接两个IP地址的网关重新分配了我的应用程序运行的网络上的客户端IP地址。因此,为了不使用*:port:*策略,我不得不查找Map到的新IP地址

ncecgwcz

ncecgwcz2#

我不能发表评论,但想回答@EthanKeiser
Visual Studio 2015为每个解决方案提供了一个单独的配置文件,您需要根据接受的答案进行更新。此文件位于:

C:\Users\\{UserName}\Documents\Visual Studio 2015\Projects\\{SolutionName}\\.vs\config\applicationhost.config
cczfrluj

cczfrluj3#

同样的问题持续了一个小时,尝试了这里的一切并与此问题相关,但修复它的是以管理员身份运行Visual Studio。否则我会一直得到Unable to connect to web server 'IIS Express'错误,这会导致你走错路。

vsdwdz23

vsdwdz234#

对于VS2019和2022,您可能会在此文件夹中找到applicationhost.config。格式似乎与上述答案相同。${projectRoot}/.vs/config/applicationhost.config

bnl4lu3b

bnl4lu3b5#

另一件帮助我的事情是将127.0.0.1添加到绑定中。
所以结果applicationhost.config看起来像这样:

<bindings>
  <binding protocol="http"  bindingInformation="*:28066:" />
  <binding protocol="http"  bindingInformation="*:28066:localhost" />
  <binding protocol="http"  bindingInformation="*:28066:127.0.0.1" />
  <binding protocol="http"  bindingInformation="*:28066:sitename.com" />

  <binding protocol="https" bindingInformation="*:44302:" />
  <binding protocol="https" bindingInformation="*:44302:localhost" />
  <binding protocol="https" bindingInformation="*:44302:127.0.0.1" />
  <binding protocol="https" bindingInformation="*:44302:sitename.com" />
</bindings>

hosts文件是这样的:

127.0.0.1   localhost
127.0.0.1   sitename.com

在添加127.0.0.1之前,在浏览器中可以像https://localhost:44302一样导航,但https://sitename.com:44302导致Bad Request
此外,如果您执行“热替换”,请确保您正在重置IIS。您可以通过在Visual Studio中右键单击项目,选择Unload project,然后在VS中更改applicationhost.config,保存它,然后在VS中更改Reload project
顺便说一下,不要忘记为您的项目生成SSL证书。我不确定Visual Studio是否会为您做到这一点。
端口应该在443004399范围内。
以下是如何生成SSL:
1.以管理员身份打开Visual Studio Developers Tools

  1. makecert -r -pe -n CN="dev.helpme.transwestern.com" -eku 1.3.6.1.5.5.7.3.1 -ss my -sr localmachine -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12
  2. netsh http show sslcert ipport=0.0.0.0:44302(您将需要从此步骤中获取appid)
  3. netsh http delete sslcert ipport=0.0.0.0:44302
    现在,打开“管理计算机证书”,导航到个人并选择新创建的。您需要“Details/Thumbprint”。
  4. netsh http add sslcert ipport=0.0.0.0:44302 certhash=<thumbprint> appid={id from step 3}(包括花苞**{}**)
    希望能帮上忙。
vhmi4jdf

vhmi4jdf6#

简单地将允许的主机添加到 * in *appsettings. local.json * 解决了我的问题&我可以看到 swagger 的页面。

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*" //<-- this one
}

但是,安全连接警告仍然存在。

相关问题