17
votes

I have created a couple of these .net core 2 web app and no issue until this one.

Running local under IISExpress it runs correctly but when I deploy a debug version to an IIS folder on the server I have issues.

When I read the config entry it is not found:

_config["MySettings:MyName"];

files contents:

appsettings.Development.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Trace",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "MySettings": {
    "MyName": "JohnDoe"
  }
}

appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Trace"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

launchSetting.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:60668/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/security",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApi.ViewerSecurity": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/security",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:60669/"
    }
  }
}

If I copy appsettings.Development.json and rename to appsettings.Production.json it works.

I changed the value of "MyName" in the prod file and logged it. Yup, reading from appsettings.Production.json.

How and why? Production isn't defined anywhere.

1

1 Answers

34
votes

The default of ConfigurationBuilder is looking for appsettings.<EnvironmentName>.json file, so based on the environment that you are working with, the name of this file is changed. and when you are in IIS Express you are in Development and when you deploy your application your environment is Production. This is why you need appsettings.Production.json.

When you are debugging there's a Environment Variable called ASPNETCORE_ENVIRONMENT which is set to Development and in your deploy where ASPNETCORE_ENVIRONMENT is not set, the default is Production