在 Visual Studio 2017 中的 launchSettings.json 中手动定义端口

Ben*_*yre 5 visual-studio visual-studio-debugging iis-express asp.net-core

当我为 Visual Studio 创建新项目时,只需复制现有项目并修改项目/解决方案/配置文件,而不是剪切并粘贴一大堆文件,然后必须设置生成操作,这要省力得多对于每个。

除了IIS 调试设置 之外,此方法运行良好。

如果我让它们保持不变,那么我一次只能调试一个项目,因为该端口将被使用。

但是,如果我更改 launchSettings.json 中的端口(我使用的是 Asp Net Core,因此 Kestrel 有一个端口,并且还有一个 SSL 端口),则没有任何效果。我现在处于这种情况,复制了一个项目并将两个端口号更改为新的随机端口,并且在相关项目中,当从菜单中选择“启动而不调试”时,我什至没有启动网络服务器(虽然我确实有一个浏览器)。

在 VS 创建的项目中,我至少得到了一个由“ASP NET Core Web Server”启动的新输出窗口,其中包含 Kestrel 输出。

我知道 VS 可能需要做一些事情,比如注册一个自签名的 SSL 证书,也许还需要修改 IIS Express 安装文件,但如果能够自己做这些事情那就太好了。

我尝试删除解决方案.vs文件夹并查看 WindowsDocuments文件夹中的 IIS Express 文件夹(其中似乎没有任何项目/解决方案特定的内容)。

这是launchSettings.json(我所做的就是更改端口号和项目配置文件名称,例如 My_AspMvc)

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost: 62649/",
      "sslPort": 44315
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "https://localhost: 44315/",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "My_AspMvc": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost: 62649/"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是设置:

VS项目道具

更新:看起来另一个关键文件是[SolutionFolder]\.vs\config\applicationhost.config

  <site name="My_AspMvc" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
      <virtualDirectory path="/" physicalPath="C:\Projects\MySoln\My_AspMvc" />
    </application>
    <bindings>
      <binding protocol="http" bindingInformation="*:62649:localhost" />
      <binding protocol="https" bindingInformation="*:44315:localhost" />
    </bindings>
  </site>
Run Code Online (Sandbox Code Playgroud)

这个难题的两个重要元素:

  • 如果您创建一个新的 ASP NET Core 项目,启动它,然后将 SSL 端口复制到现有项目,该端口将从此开始工作。它显然是在系统全局的某个地方注册的。如果不创建新项目,该端口将无法工作。

  • 请注意,模仿整个过程的一个简单方法是在现有的工作进程中打开launchSettings.jsonSSL 端口号并随机更改几位数字。这是行不通的。

Ben*_*yre 8

好的,终于找到了这个:Where do I indicates my SSL port for IISExpress?

注意到:https: //learn.microsoft.com/en-us/iis/extensions/using-iis-express/running-iis-express-without-administrative-privileges

...SSL 端口必须在 44300 和 44399 之间。我的问题是,我没有遵守这个范围。

对所有这些发现进行快速总结:

  • Visual Studio 使用 IIS Express 调试 ASP NET 项目
  • SSL 没有特定于端口的全局配置,下面总结的除外
  • IIS Express 有一个默认配置文件 C:\Users\[UserName]\Documents\IISExpress\config\applicationhost.config
  • 当 VS 启动 IIS Express 时,它会传递一个本地项目特定的配置文件来取代默认的配置文件:[SolutionFolder]\.vs\config\applicationhost.config
  • SSL 端口必须介于 44300 和 44399 之间,并在 [SolutionFolder]\.vs\config\applicationhost.config [ProjectFolder]\Properties\launchSettings.json
  • 看来您实际上可以删除整个内容[SolutionFolder]\.vs\,并将在下一次调试启动时重建它(所以这是一个选项,而不是编辑)
  • 应用程序的实际 (Kestrel HTTP) 端口似乎与 launchSettings 或 applicationHost 中指定的端口无关,但在 Web 服务器启动输出窗口中可见