Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Net Core 2.0 looking for appsettings.Production.json

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.

like image 439
Gina Marano Avatar asked Jun 14 '18 03:06

Gina Marano


People also ask

Where is Appsettings production json?

appsettings. json is one of the several ways, in which we can provide the configuration values to ASP.NET core application. You will find this file in the root folder of our project. We can also create environment-specific files like appsettings.

How do I add Appsettings production json in Visual Studio?

First, in Visual Studio you will have to right click your project name and add a new item. Inside 'Add new Item' window chose 'Scripts' on the left column, and look for JavaScript JSON Configuration File template. Name it as appsettings. Production.


1 Answers

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

like image 87
Kahbazi Avatar answered Oct 26 '22 07:10

Kahbazi