在 IIS 中托管 asp.net 核心应用程序以从网络内的其他 PC 访问

Host asp.net core app in IIS to access from other PC's inside the network

我如何在 Windows IIS 中托管我的 Asp.Net 核心(1、2 或 2.1)网络应用程序,以便能够从我们本地网络(或我的虚拟 OS)?我的电脑是台式机 Windows 10 Pro(不是 Win Server)。

我已按照 Host ASP.NET Core on Windows with IIS 文章进行操作,但我无法使其正常工作。

我根据那篇文章所做的如下:

确保先决条件:Visual Studio 2017,安装 Asp.Net Core 2.1 SDK,从 Turn Windows features on or off、Asp.Net Core Hosting Bundle Installer from here 添加 Internet 信息服务 (IIS)(在 IIS 中验证 AspNetCoreModule 模块)然后:

将 Visual Studio 中的默认 asp.net 核心应用程序(构建 > 发布 > 文件夹)发布到文件夹并将其内容复制到 D:\publish1

在 IIS 中,right-click 站点 > 添加网站...并使用这些值(根据显示的图片 there):

  [Site name]: mysite_com
  [Physical path]: `D:\publish1`
  [IP address]: `All Unassigned`
  [Port]: 80
  [Host name]: mysite.com

同时将 Application Pools > mysite_com > Basic Settings > .Net CLR Version 更改为 No Managed Code

现在,当我测试 http://[IP of my PC]/mysite.com 或类似地址时,无法在浏览器中访问我的 Web 应用程序。

那么,这些 IIS 设置有什么问题?

编辑: 好像不是.net core 2.1特有的(换了文章的.net core版本主要是一样的)所以我修改了标题和问题。

我找到了解决方案并想在这里记录它,因为它可能对其他人有帮助。有趣的是,Microsoft doc 并未解释此类常见场景的所有细节,例如当您想要 运行 您在 Intranet 网络中使用 Web 应用程序时,例如建造大型办公室。这是所需的 IIS 设置:

  1. 确保您有 IIS(打开或关闭 windows 功能 > 互联网信息服务)

  2. 确保 Asp.Net Core Host Bundle 已安装:dotnet-hosting-2.1。1.exe:IIS 中的 AspNetCoreModule 可以从 IIS 管理器 > 网站 > 任何网站进行验证,例如默认网站 > 模块。

  3. 将 asp.net 中的核心应用程序发布到 Visual Studio(构建 > 发布 > 文件夹)到一个文件夹,然后将其生成的文件复制到其他所需的目标路径(例如 D: \publish1) 以避免下一次代码更新的文件锁定问题。

  4. 在 IIS 中,选择应用程序池(从站点上方的左侧树视图),右键单击 > 选择添加应用程序池 > 名称:myAppPool,.Net CLR 版本:无托管代码。 (对于 .Net 核心应用程序)

  5. 在命令提示符中找到您的 IP 地址:运行 ipconfig(在 windows10 中,它在以太网适配器下:IPv4 Address,例如:192.168.103.xyz).

现在,有两个选项:A) 添加网站,或 B) 在默认网站中添加应用程序

A) 添加网站 => http://192.168.103.xyz:890

在 IIS 管理器中,从左侧树视图中选择 "Sites" 节点 > 右键单击​​ > 添加网站... >

[Site name]: myApp
[Application Pool]: Select:myAppPool as specified in step 4. if you didn't create it in before, IIS will add a new pool based on your SiteName which you can edit its Basic Settings later in Application Pools tree view node to set its .Net CLR Version to No Managed Code. 

[Physical path]: path to your published files (eg: D:\publish1)

[IP address]: can change it to your IP (from drop down) or left it to `All Unassigned`

![Port]: must be something other than 80! (eg: 890), it must be a free port not taken by other apps. you can test different values if some fails. in my case, port 4, 100, 200, 1100 works; but 80, 110 (pop3 email) or 3000 not works as they are used/reserved for other means.

![Host name]: left it blank if you want to access this app from your network! It is required for real internet world wide web sites!

  • 您可能在这里错过的关键设置是 端口(80 除外) 主机名(空字符串) 如上所述!检查 this link 以了解不同的端口号:系统端口 (0-1023)、用户端口 (1024-49151)、Dynamic/Private 通常未分配的端口 (49152-65535)。

  • 您可以为站点设置超过 1 个端口号或 IP:从右侧面板中选择 [绑定] 并编辑或添加条目。

  • 允许您选择的端口号通过您的防火墙,以便能够从外部 PC 访问它。对于 windows 防火墙,请转到:Windows Firewall with Advanced Security > Inbound Rules > New rule > [Port] option,指定您的端口号 > ...

  • 浏览:192.168.103.xyz:890(使用您的 IP 地址和选择的端口号)。如果您没有为您的站点设置特定 IP(即 All Unassigned),那么您也可以浏览 localhost:890 以查看您的应用程序。

B) 添加应用程序(在默认网站内)=> http://192.168.103.xyz/myApp

  • 在 IIS 管理器中,从左侧树视图中选择 "Default Web Site",右键单击 > 添加应用程序 > 为 [别名] 选择一个名称:(例如:myApp),[应用程序池] : Select:myAppPool 如第4步所指定, [物理路径]: 你的发布路径(eg: D:\publish1)

  • 浏览:localhost/myApp 在您自己 PC 的浏览器中测试您是否看到您的 Web 应用程序。

  • 浏览:192.168.103.xyz/myApp(使用您的 IP 地址)如果您想从网络上的其他 PC 或虚拟 PC 获取它。