Search code examples
c#.netiisdeploymentappsettings

How to use different appsettings.json file for different deployment?


I have .NET Core 7 WEB API project. In there, I have 3 settings files. More pecisely:

  • appsettings.json
  • appsettings.QA.json
  • appsettings.Production.json
  • appsettings.Development.json.

The behavior I want to achieve is:

When I run: dotnet publish -c QA -o deploy and host the application on IIS, it is using appsettings.QA.json file.

What is happening:

The application always uses appsettings.Production.json as default configuraiton.

Am I missing something ?


Solution

  • When you publish your application using dotnet publish -c QA -o deploy, the -c QA flag specifies that the build should use the QA configuration, but it doesn't set the ASPNETCORE_ENVIRONMENT variable to QA.

    To achieve this in the iis you can follow below steps:

    1. Open the IIS Manager, Select your application/site from the left pane
    2. Open the Configuration Editor
    3. From the Section dropdown, select system.webServer/aspNetCore.
    4. In the environmentVariables collection, add an entry with name as ASPNETCORE_ENVIRONMENT and value as QA.

    after making changes the web.config will look like this:

    <configuration>
      <system.webServer>
        <aspNetCore processPath="dotnet" arguments=".\App.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout">
          <environmentVariables>
            <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="QA" />
          </environmentVariables>
        </aspNetCore>
      </system.webServer>
    </configuration>