How to deal with environment differences when deploying asp.net core application?

The central configuration file is the appsettings.json and you can have multiple files, like appsettings.Production.json etc, which will be loaded and override settings from the appsettings.json.

For example

        // Set up configuration sources.
        var builder = new ConfigurationBuilder()
            .SetBasePath(hostEnv.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{hostEnv.EnvironmentName}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables();

All you need to get this working is the environment variable for setting the environment type (see documentation here).

You can also have environment variables that override, if you add AddEnvironmentVariables() to your configuration builder. So if you have a appsettings.json of

{
    "Data"  {
         "Default" {
              "ConnectionString" : "..."
         }
    }
}

and want to override that via environment Variable, you'd set up an environment variable called "Data:Default:ConnectionString" and it's value will override the settings in the appsettings.config and appsettings.Production.config (assuming your .AddEnvironmentalVariables() is called after .AddJsonFile() - Last registration with the matching key wins) with the value from the environment variable.

You can find more in the official documentation here.

Update

Since in the comments some understand this as the only way to set the environment, there are many ways to set environment variable (most of it is documented in Use multiple environments in ASP.NET Core), all ultimately boiling down to being an environment variable, just within a different scope:

  1. Environment Variable (globally, Windows cmd.exe set ASPNETCORE_ENVIRONMENT=Development or $Env:ASPNETCORE_ENVIRONMENT = "Development" on powershell, export ASPNETCORE_ENVIRONMENT = Development on linux)
  2. Per command environment variable (i.e. linux: ASPNETCORE_ENVIRONMENT=Production dotnet MyApp.dll)
  3. Docker container, i.e. via docker-compose.yaml

    web:
        environment:
        - ASPNETCORE_ENVIRONMENT=Debugging
    
  4. Docker container via command line docker run -e ASPNETCORE_ENVIRONMENT=Debugging
  5. in IIS via web.config.

    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="true" >
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
    </aspNetCore>
    
  6. On IIS set it per AppPool (see here)
  7. On Linux via service definition files (see docs)
  8. Azure App Service via Environment variables, can be set per slot and having different slots for Staging, Development, Production and i.e. deploying to staging, doing warm up and swapping with Production
  9. Per execution via dotnet run --launch-profile Development

They all change/set the environment variable in a specific scope (globally, locally to a container, inside the app pool, per execution etc.). Choose one which suits your needs.