首页 > 解决方案 > 如何在 ASP.NET Core 3.1 应用程序中根据 URL 进行单独的 IIS Express 配置?

问题描述

我有一个项目,我想在 IIS 服务器上的两个不同地址发布:https://domain/test1/https://domain/test2/. 根据我发布的地址,我想为我的项目设置不同的配置设置,为简化起见,假设配置是一个 entry email。因此,对于https://domain/test1/我想要配置的地址email: test1@test1.comhttps://domain/test2/, email: test2@test2.com

我尝试更改launchSettings.json文件中的配置,但我无法添加具有相同名称的条目,如果我更改条目的名称,那么它将不知道使用 IISExpress 发布它。

    "IIS Express": {
       "commandName": "IISExpress",
       "launchBrowser": true,
       "applicationUrl": "https://domain/test1",
       "email": "test1@test1.com",
       "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
       }
    },
    "IIS Express": {
       "commandName": "IISExpress",
       "launchBrowser": true,
       "applicationUrl": "https://domain/test2",
       "email": "test2@test2.com",
       "environmentVariables": {
           "ASPNETCORE_ENVIRONMENT": "Development"
       }
    },

如何根据 ASP.NET Core 3.1 中的当前 URL 地址进行单独配置?

标签: asp.net-coreurlconfigurationiis-express

解决方案


据我所知,launchSettings.json文件只用在本地开发机上,没有部署。它仅由 Visual Studio 使用。

所以如果你在launchSettings.json中设置了email设置,就不会部署到生产服务器,也不会被使用。

此外,正如 Lex 所说,没有这样的功能可以满足您的要求。

我建议您可以考虑使用 appsetting.json 和 Environment 变量来满足您的要求。

您可以使用不同的环境创建多个 appsetting.json。

在此处输入图像描述

发布后,您可以打开applicationHost.config指定站点位置并设置不同的环境变量,如下所示。

注意:此解决方法仅适用于不同的 IIS 网站名称,路径为 IIS 网站名称。

applicationHost.config路径:C:\Windows\System32\inetsrv\config\

<location path="domainA">
    <system.webServer>
        <aspNetCore>
            <environmentVariables>
                <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging" />
            </environmentVariables>
        </aspNetCore>
    </system.webServer>
</location>
<location path="domainB">
    <system.webServer>
        <aspNetCore>
            <environmentVariables>
                <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" />
            </environmentVariables>
        </aspNetCore>
    </system.webServer>
</location>

推荐阅读