How do I transform appsettings.json in a .NET Core MVC project?
Determine EnvironmentName from Build Type
For anybody that would like to set the EnvironmentName based on the build type, there is the handy .UseEnvironment(environmentName)
on WebHostBuilder
(found in Program Main).
As long as the appropriate compilation symbols are set against the build configurations in your project, you can do something like this to determine the EnvironmentName:
public static void Main(string[] args)
{
string environmentName;
#if DEBUG
environmentName = "Development";
#elif STAGING
environmentName = "Staging";
#elif RELEASE
environmentName = "Production";
#endif
var host = new WebHostBuilder()
.UseKestrel()
.UseEnvironment(environmentName)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
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.
The answer is here
The solution is to setup different environment variable values on each IIS site for the key ASPNETCORE_ENVIRONMENT
The steps to do so are:
- 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
Alternatively, you can modify your applicationHost.config
file (normally located at C:\Windows\System32\inetsrv\config\applicationHost.config
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>
If you are using the default code in Program.cs
, you don't need to do anything beyond creating the two files in the project.
The default code in Program.cs
looks like this:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
Here's what that's actually doing:
public static IWebHostBuilder CreateDefaultBuilder(string[] args)
{
var builder = new WebHostBuilder();
...
builder.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
});
...
return builder;
}
env.EnvironmentName
is set to the value of the ASPNETCORE_ENVIRONMENT
environment variable, so all you have to do is create the appsettings.{ASPNETCORE_ENVIRONMENT}.json
file and it will automatically get merged.
Additional note: to get the two files to actually merge, use this syntax:
var appSettings = Configuration.GetSection("AppSettings").Get<AppSettings>();
not:
var appSettings = new AppSettings();
Configuration.Bind("AppSettings", appSettings);
return appSettings;
The latter will not returned the merged data.
Thanks to Shawn Wildermuth for this.
The dialog you linked in the picture is only to configure "launchSettings.json". This file is not used by your application.
It is only used by Visual Studio to set the environment and open an URL in the browser when you hit F5 and nothing else.
When you want to switch environments, you need to setup an environment variable before launching. How to do this, depends on your environment.
Windows (Commandline, cmd.exe)
setx ASPNETCORE_ENVIRONMENT "Development"
Windows (Powershell)
$Env:ASPNETCORE_ENVIRONMENT = "Development"
Linux
export ASPNETCORE_ENVIRONMENT="Development"
Linux (Set it for a single command)
ASPNETCORE_ENVIRONMENT="Development" dotnet run
Update in regards to the comment
Yes it is machine specific (except for Linux, which you can do per command). However, in IIS you can do that too either via different app pools or by following this answers instructions to add it to IIS