如何设置 ASPNETCORE_ENVIRONMENT 以考虑发布 ASP.NET Core 应用程序 [英] How to set ASPNETCORE_ENVIRONMENT to be considered for publishing an ASP.NET Core application

查看:20
本文介绍了如何设置 ASPNETCORE_ENVIRONMENT 以考虑发布 ASP.NET Core 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将我的 ASP.NET Core Web 应用程序发布到我的本地文件系统时,它总是采用 production-config 和值为 =Production"的 ASPNETCORE_ENVIRONMENT 变量.

When I publish my ASP.NET Core web application to my local file system, it always takes the production-config and the ASPNETCORE_ENVIRONMENT variable with the value = "Production".

我必须如何以及在何处设置 ASPNETCORE_ENVIRONMENT 变量的值,以便它不仅用于调试,而且也用于发布?我已经尝试了以下选项但没有成功:

How and where do I have to set the value of the ASPNETCORE_ENVIRONMENT variable so that it will be considered not only for debugging, but also for the publishing? I already tried the following options without success:

  • 在 Windows 设置中
  • 在文件 .pubxml 文件中
  • 在文件 launchSettings.json
  • 在文件 project.json

推荐答案

除了上面提到的选项,还有其他几个解决方案.

Other than the options mentioned above, there are a couple of other solutions.

1.修改工程文件(.CsProj)文件

MSBuild 支持 EnvironmentName 属性,它有助于根据您希望部署的环境设置正确的环境变量.环境名称将在发布阶段添加到 web.config 中.

MSBuild supports the EnvironmentName property which can help to set the right environment variable as per the environment you wish to deploy. The environment name would be added in the web.config during the publish phase.

只需打开项目文件 (*.csProj) 并添加以下 XML.

Simply open the project file (*.csProj) and add the following XML.

<!-- Custom property group added to add the environment name during publish

     The EnvironmentName property is used during the publish
     for the environment variable in web.config
-->
<PropertyGroup Condition=" '$(Configuration)' == '' Or '$(Configuration)' == 'Debug'">
  <EnvironmentName>Development</EnvironmentName>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' != '' AND '$(Configuration)' != 'Debug' ">
  <EnvironmentName>Production</EnvironmentName>
</PropertyGroup>

上面的代码会将环境名称添加为 Development 以用于调试配置或未指定配置.对于任何其他配置,环境名称将是生成的 web.config 文件中的 Production.更多细节是 此处.

The above code would add the environment name as Development for a debug configuration or if no configuration is specified. For any other configuration, the environment name would be Production in the generated web.config file. More details are here.

2.在发布配置文件中添加 EnvironmentName 属性.

我们也可以在发布配置文件中添加 属性.打开位于 Properties/PublishProfiles/{profilename.pubxml} 的发布配置文件.这将在项目发布时在 web.config 中设置环境名称.更多详细信息在此处.

We can add the <EnvironmentName> property in the publish profile as well. Open the publish profile file which is located at Properties/PublishProfiles/{profilename.pubxml}. This will set the environment name in web.config when the project is published. More details are here.

<PropertyGroup>
  <EnvironmentName>Development</EnvironmentName>
</PropertyGroup>

3.使用 dotnet publish

此外,我们可以将属性 EnvironmentName 作为命令行选项传递给 dotnet publish 命令.以下命令将环境变量作为 Development 包含在 web.config 文件中.

Additionally, we can pass the property EnvironmentName as a command-line option to the dotnet publish command. The following command includes the environment variable as Development in the web.config file.

dotnet publish -c Debug -r win-x64/p:EnvironmentName=Development

这篇关于如何设置 ASPNETCORE_ENVIRONMENT 以考虑发布 ASP.NET Core 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆