如何临时交换端口以使用自定义 URL 启动,然后将端口交换回 asp.net MVC Core 中的原始端口?
How can I temporarily swap ports to launch with a custom URL, then swap ports back to original in asp.net MVC Core?
我在同一个 Visual Studio 2017 解决方案中有 2 个 asp.net MVC Core Web 项目,我需要使用我的自定义 Launch URL 在浏览器中单独启动它们。首先,我将以下内容添加到我的主机文件中:
127.0.0.1 myurl.mybiz.org
最初,我成功地将 Project1 > Properties > Debug > Launch URL: 设置为 https://myurl.mybiz.org and left everything else alone. I was able to launch Project1 at https://myurl.mybiz.org IE11.
接下来,我想在 https://myurl.mybiz.org 启动 Project2,所以我将启动 URL 从Project1 并将其粘贴到 Project2 > 属性 > 调试 > 启动 URL:。那没有用,我收到 "This page can't be displayed" 错误。
所以我在 Web 服务器设置 > 应用 URL: 下在 Project1 和 [=37= 之间交换了本地主机端口]项目 2。最初,Project1 > Properties > Debug > App URL: 是 http://localhost:12345/,并且 Project2 > Properties > Debug > App URL: 是 http://localhost:54321/,所以我将 Project1 设置为 54321 并将 Project2 设置为 12345,这有效。我能够在 https://myurl.mybiz.org.
启动 Project2
当我准备再次启动 Project1 在我的自定义 url 时,我切断了 Launch URL:https://myurl.mybiz.org 从 Project2 并将其粘贴回 Project1,并将本地主机端口交换回其原始编号:Project1 > Properties > Debug > App URL: http://localhost:12345/ and Project2 > Properties > Debug > 应用 URL:http://localhost:54321/。但是 Project1 不会在 https://myurl.mybiz.org 启动,我只是得到错误 "This page can't be displayed." 之后,当我将所有内容切换回当 Project2 在我的自定义 url 成功启动时它是如何工作的,Project2 仍然在 [=16= 成功启动].
为什么以这种方式更改设置后,Project1 不再以我的自定义 url 启动?
这是我的 2 个 launchSettings.json 文件,在我的自定义启动 url 切换回 运行 Project1 之后,运行 ]ning Project2 在自定义 url(VS2017 表示在切换回这些设置后与原始设置没有变化):
项目 1:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:12345/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "https://myurl.mybiz.org/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Project1": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
项目 2:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:54321/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Project2": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
交换端口然后像这样交换回来并没有更新 IIS Express applicationhost.config 文件,这阻止了我的网站以我的自定义 url 启动。找到 applicationhost.config 后,我发现绑定明显错误,需要更新。
这就是我要恢复到原来状态所需要的:
<sites>
. . .
<site name="Project1" id="2">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Repos\Projects\Project1" />
</application>
<bindings>
<binding protocol="https" bindingInformation="*:443:myurl.mybiz.org" />
<binding protocol="http" bindingInformation="*:12345:localhost" />
</bindings>
</site>
<site name="Project2" id="3">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Repos\Projects\Project2" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:54321:localhost" />
</bindings>
</site>
. . .
</sites>
但这是我手动修复之前绑定的实际情况:
<sites>
. . .
<site name="Project1" id="2">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Repos\Projects\Project2" />
</application>
<bindings>
<binding protocol="https" bindingInformation="*:443:myurl.mybiz.org" />
<binding protocol="http" bindingInformation="*:54321:localhost" />
</bindings>
</site>
<site name="Project2" id="3">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Repos\Projects\Project1" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:12345:localhost" />
</bindings>
</site>
. . .
</sites>
我在同一个 Visual Studio 2017 解决方案中有 2 个 asp.net MVC Core Web 项目,我需要使用我的自定义 Launch URL 在浏览器中单独启动它们。首先,我将以下内容添加到我的主机文件中:
127.0.0.1 myurl.mybiz.org
最初,我成功地将 Project1 > Properties > Debug > Launch URL: 设置为 https://myurl.mybiz.org and left everything else alone. I was able to launch Project1 at https://myurl.mybiz.org IE11.
接下来,我想在 https://myurl.mybiz.org 启动 Project2,所以我将启动 URL 从Project1 并将其粘贴到 Project2 > 属性 > 调试 > 启动 URL:。那没有用,我收到 "This page can't be displayed" 错误。
所以我在 Web 服务器设置 > 应用 URL: 下在 Project1 和 [=37= 之间交换了本地主机端口]项目 2。最初,Project1 > Properties > Debug > App URL: 是 http://localhost:12345/,并且 Project2 > Properties > Debug > App URL: 是 http://localhost:54321/,所以我将 Project1 设置为 54321 并将 Project2 设置为 12345,这有效。我能够在 https://myurl.mybiz.org.
启动 Project2当我准备再次启动 Project1 在我的自定义 url 时,我切断了 Launch URL:https://myurl.mybiz.org 从 Project2 并将其粘贴回 Project1,并将本地主机端口交换回其原始编号:Project1 > Properties > Debug > App URL: http://localhost:12345/ and Project2 > Properties > Debug > 应用 URL:http://localhost:54321/。但是 Project1 不会在 https://myurl.mybiz.org 启动,我只是得到错误 "This page can't be displayed." 之后,当我将所有内容切换回当 Project2 在我的自定义 url 成功启动时它是如何工作的,Project2 仍然在 [=16= 成功启动].
为什么以这种方式更改设置后,Project1 不再以我的自定义 url 启动?
这是我的 2 个 launchSettings.json 文件,在我的自定义启动 url 切换回 运行 Project1 之后,运行 ]ning Project2 在自定义 url(VS2017 表示在切换回这些设置后与原始设置没有变化):
项目 1:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:12345/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "https://myurl.mybiz.org/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Project1": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
项目 2:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:54321/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Project2": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
交换端口然后像这样交换回来并没有更新 IIS Express applicationhost.config 文件,这阻止了我的网站以我的自定义 url 启动。找到 applicationhost.config 后,我发现绑定明显错误,需要更新。
这就是我要恢复到原来状态所需要的:
<sites>
. . .
<site name="Project1" id="2">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Repos\Projects\Project1" />
</application>
<bindings>
<binding protocol="https" bindingInformation="*:443:myurl.mybiz.org" />
<binding protocol="http" bindingInformation="*:12345:localhost" />
</bindings>
</site>
<site name="Project2" id="3">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Repos\Projects\Project2" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:54321:localhost" />
</bindings>
</site>
. . .
</sites>
但这是我手动修复之前绑定的实际情况:
<sites>
. . .
<site name="Project1" id="2">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Repos\Projects\Project2" />
</application>
<bindings>
<binding protocol="https" bindingInformation="*:443:myurl.mybiz.org" />
<binding protocol="http" bindingInformation="*:54321:localhost" />
</bindings>
</site>
<site name="Project2" id="3">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Repos\Projects\Project1" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:12345:localhost" />
</bindings>
</site>
. . .
</sites>