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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With