如何在.NET Core MVC项目中转换appsettings.json? [英] How do I transform appsettings.json in a .NET Core MVC project?
问题描述
我已将其他json配置文件添加到我的项目中
I've added additional json config files to my project
appsettings.DEV.json
appsettings.QA.json
并根据环境将它们加载到Startup
函数中:
and loaded them in the Startup
function based on the environment:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
...
我了解如何更改环境:在项目属性中修改ASPNETCORE_ENVIRONMENT
环境变量的值.但是,似乎无法根据配置指定不同的环境变量,下拉列表标记为"N/A"并被禁用.
And I understand how to change the environment: modify the value of the ASPNETCORE_ENVIRONMENT
environment variable in project properties. However, there does not appear to be the ability to specify different environment variables depending on the configuration, the dropdownlist is labeled "N/A" and disabled.
我看到的唯一选择是手动更改环境变量值,以更改使用哪些设置.我敢肯定有一种方法可以自动执行此操作,否则您将如何使用CI? (除了使用脚本来更改环境变量外,还必须有一种更简单的方法.)
The only option I see is to manually change the environment variable value, to change which appsettings are used. I'm sure there is a way to do it automatically, or else how would you ever use CI? (other than using a script to change the environment variable, there has to be an easier way).
此处的目标是为DEV,QA和PROD这三种环境设置自动构建和持续集成. DEV和QA位于同一台计算机上,因此请进行设置不能选择手动指定环境的环境变量.
The goal here is to setup automated builds and continuous integration for three environments: DEV, QA, and PROD. DEV and QA are on the same machine, so setting the environment variable that specifies the environment manually is not an option.
推荐答案
我从Tsengs的答案中找到了一个解决方案,但希望在此进行描述以求清楚. 在另一个问题的答案中找到了解决方案,但是问题却大不相同(并且我也扩展了答案),所以我不认为该问题应被标记为重复.
I've found a solution from Tsengs answer but wish to describe it here for clarity. The solution is found in the answer to another question however the question is quite different (and I've also expanded upon the answer) so I do not believe this question should be marked as a duplicate.
答案是此处
解决方案是在每个IIS站点上为键ASPNETCORE_ENVIRONMENT
The solution is to setup different environment variable values on each IIS site for the key ASPNETCORE_ENVIRONMENT
要执行的步骤是:
- 转到IIS中的应用程序,然后选择
Configuration Editor
. - 选择
Configuration Editor
- 在
Section
组合框中选择system.webServer/aspNetCore
(RC2和RTM)或system.webServer/httpPlatform
(RC1) - 在
From
组合框中选择Applicationhost.config ...
. - 单击
enviromentVariables
元素并打开编辑窗口. - 设置环境变量.
- 关闭窗口,然后单击应用".
- 完成
- Go to your application in IIS and choose
Configuration Editor
. - Select
Configuration Editor
- Choose
system.webServer/aspNetCore
(RC2 and RTM) orsystem.webServer/httpPlatform
(RC1) inSection
combobox - Choose
Applicationhost.config ...
inFrom
combobox. - Click on
enviromentVariables
element and open edit window. - Set your environment variables.
- Close the window and click Apply.
- Done
或者,您可以修改您的applicationHost.config
文件(通常位于C:\Windows\System32\inetsrv\config\applicationHost.config
Alternatively, you can modify your applicationHost.config
file (normally located at C:\Windows\System32\inetsrv\config\applicationHost.config
并在根<Configuration>
标记下添加以下条目,其中"my-iis-site"是您的IIS站点的名称.
And add the following entry under the root <Configuration>
tag, where "my-iis-site" is the name of your IIS site.
<location path="my-iis-site">
<system.webServer>
<aspNetCore>
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="DEV" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</location>
这篇关于如何在.NET Core MVC项目中转换appsettings.json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!