配置 Visual Studio 的 IIS Express 运行的站点局域网访问

Visual Studio 启动的站点,默认运行在 IIS Express 中,而 IIS Express 默认配置是只支持 localhost 访问的,因此不支持局域网访问,如果在局域网中另一台电脑通过 IP 来访问,会提示如下:

1
2
Bad Request - Invalid Hostname
HTTP Error 400. The request hostname is invalid.

开启局域网访问

要让站点局域网能够访问,请按如下步骤操作:

  1. VS 运行启动站点后,右键桌面状态栏的 IIS Express,点击你运行的站点,点击配置的路径,会自动打开 applicationhost.config 文件。

  2. 定位到 <site><bindings> 元素,并复制该元素下的 bingding 元素,粘贴到 bindings 元素下。

    并将属性 bindingInformation 的值中的 localhost 修改为本机 IP。

    1
    2
    3
    4
    <bindings>
    <binding protocol="http" bindingInformation="*:52748:localhost" />
    <binding protocol="http" bindingInformation="*:52748:10.211.55.21" />
    </bindings>

    如果使用 Rider,上面的配置可能会丢失,需要在 Project Proerties 》web》 去掉Generate applicationhost.config。

  3. 关闭 VS,重新以管理员身份运行 VS,这一步非常重要。(Rider 不需要以管理员身份运行。)

  4. 再次运行程序,在 IIS Express 中就能看到带本机 IP 的 URL 就表示可以通过局域网访问了。

问题

Failed to register URL

1
2
Failed to register URL "http://10.211.55.21:52748/" for site "ZhouShanWebService" application "/". Error description: 拒绝访问
。 (0x80070005)
  1. 以管理员方式打开 PowerShell。

  2. 执行如下命令(将 IP 和端口换成你的),以允许本地服务注册 10.211.55.21 的 5000 端口。

    1
    netsh http add urlacl url=http://10.211.55.21:5000/ user=everyone

    查看或删除注册的端口用如下命令:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    PS C:\Windows\system32>  netsh http delete urlacl url=http://10.211.55.3:52748/

    已成功删除 URL 保留项

    PS C:\Windows\system32> netsh http show urlacl

    URL 保留:
    -----------------

    保留的 URL : http://*:5357/
    用户: BUILTIN\Users
    侦听: Yes
    委派: No
    用户: NT AUTHORITY\LOCAL SERVICE
    侦听: Yes
    委派: No
    SDDL: D:(A;;GX;;;BU)(A;;GX;;;LS)

配置 Visual Studio 的 IIS Express 运行的站点局域网访问

https://ganzhixiong.com/p/e792489/

Author

干志雄

Posted on

2021-12-01

Updated on

2021-12-01

Licensed under

Comments