Search code examples
asp.net-coreweb-deploymentmsdeploy

ASP.NET Core deploy only applicable appsettings.{EnvironmentName}.json


I've made my first proof of concept ASP.NET Core application, I have 5 appsettings files:

  1. appsettings.json
  2. appsettings.Development.json
  3. appsettings.Test.json
  4. appsettings.Staging.json
  5. appsettings.Production.json

I'm running the app in IIS so I actually have web.config files so that I can set the ASPNETCORE_ENVIRONMENT environment variable on a per application basis:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.webServer>

    <aspNetCore processPath="dotnet" >
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development"></environmentVariable>
      </environmentVariables>
    </aspNetCore>

  </system.webServer>

</configuration>

I have 4 web.config transforms so I can publish from Visual studio, and it will set the correct environment variable value for each environment.

When I publish to each environment though it publishes all the appsettings files. Is it possible to get it to publish on the root appsettings.json and the applicable environment specific one, but omit the others?


Solution

  • No, it's not possible. ASP.NET Core is not like ASP.NET. In ASP.NET, you literally published for the environment; if you wanted to switch to a different environment, you'd need to republish. ASP.NET Core is published for all environments. The same published app can be picked up and moved to any environment, without change. The actual environment is generally externalized, such as via and environment variable, and can be changed on a whim, without requiring new code to be deployed. This is actually a feature of ASP.NET Core.

    Now, the way you're handling the environment variable does somewhat make it dependent on the publish, but that's just modifying the web.config, which itself only has meaning when deploying to IIS. ASP.NET Core itself doesn't care about or use web.config, and honestly doesn't even care about or use release configurations.