1

I'm trying to define some environment variables in my web application. I host my site on Azure which has a staging deployment slot that the site goes to first, then, if the changes are signed off it's released to production.

The URLs for these two places are:

Production

"BaseUrl": "https://mycompany.azurewebsites.net"

Staging

"BaseUrl": "https://mycompany-staging2n1h.azurewebsites.net"

I want my appsettings.Staging.json file to be used when the site hits the staging area and then once it's released to production I want it to use appsettings.json.

It's my understanding that, if you don't define a production appsettings.Production.json file in your applicataion it defaults to appsettings.json.

So, I created a appsettings.Staging.json file within my application which contains a different connection string from the production environment.

appsettings.json

{
  "AzureAd": {
    "CallbackPath": "/signin-oidc",    
    "BaseUrl": "https://mycompany.azurewebsites.net"       
  },      
  "ConnectionStrings": {
    "MyCompanyConnection": "production connection string"        
  },
  "AllowedHosts": "*"
}

appsettings.Staging.json

{
  "AzureAd": {
    "CallbackPath": "/signin-oidc",
    "BaseUrl": "https://mycompany-staging2n1h.azurewebsites.net"
  },     
  "ConnectionStrings": {
    "MyCompanyConnection": "staging connection string"
  },
  "AllowedHosts": "*"
}

I then went into the launchSettings.json file to set this up as follows:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:xxxx",
      "sslPort": xxxx
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "MyCompany": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": { 
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    },
    "MyCompany Staging": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      }
    }
  }
}

When I deploy my application to Azure and it hits the staging area, it's looking at the wrong connection string which means it's not using the correct appsettings file. Have I missed a step or set something up incorrectly?

1
  • Its up to you of course, but I find it easier to use the app service configuration. Those are pulled in as environment variables and will override anything in a json file. Commented Apr 16, 2021 at 11:59

1 Answer 1

5

You need to set the the ASPNETCORE_ENVIRONMENTon Azure - for an app service, it's in the Configuration section:

Image of app service configuration showing environment

(Changes to launchSettings.json have no effect when an app is deployed, it's only used by Visual Studio)

As the documentation says:

To determine the runtime environment, ASP.NET Core reads from the following environment variables:

  1. DOTNET_ENVIRONMENT
  2. ASPNETCORE_ENVIRONMENT when ConfigureWebHostDefaults is called. The default ASP.NET Core web app templates call ConfigureWebHostDefaults. The ASPNETCORE_ENVIRONMENT value overrides DOTNET_ENVIRONMENT

The following values are provided by the framework:

  • Development : The launchSettings.json file sets ASPNETCORE_ENVIRONMENT to Development on the local machine.
  • Staging
  • Production : The default if DOTNET_ENVIRONMENT and ASPNETCORE_ENVIRONMENT have not been set.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.