I'm using Azure DevOps for building and releasing a .NET Core MVC web application to a Windows Server 2016 EC2 instance in AWS.
I have different environments, so I've created the following appsettings.json files:
- appsettings.DEV.json
- appsettings.STG.json
- appsettings.PRD.json
After some research, I see that we can set the ASPNETCORE_ENVIRONMENT variable in the web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\www.MyApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="[ENV]" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</location>
</configuration>
I can then load the respective environment appsetting.json file using the following code in Program.cs:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(ConfigConfiguration)
.UseStartup<Startup>();
static void ConfigConfiguration(WebHostBuilderContext ctx, IConfigurationBuilder config)
{
config.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{ctx.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
}
}
For each deployment, I would like to control the web.config that gets deployed so that I can control the value of ASPNETCORE_ENVIRONMENT. Similar to web.config transform in a traditional ASP.NET environment.
Is there a way to do this in Azure DevOps, or via a setting in Visual Studio? I've read that .NET Core 2.2 will offer a solution for this, but what can be done in the meantime?