With .NET Core or ASP.NET Core we don't use web.configs or app.configs for that purpose anymore. There is a new configuration-system built by Microsoft. By default, the webhost builds a configuration that includes these in that order
- appsettings.json, optional
- appsettings.{environment}.json, optional
- environment-variables
In your case you will want to build one manually.
For that you need to have a reference to (included by default for visual studio templates via meta-package)
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Configuration.Json
- Microsoft.Extensions.Configuration.EnvironmentVariables
Now instead of using the default configuration, build your own within your startup class.
public class Startup
{
// environment is being injected by the webhost
public Startup(IHostingEnvironment environment)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile(Path.Combine(AppContext.BaseDirectory, "appsettings.json"), optional: false) // u can change that if you want to be optional
.AddJsonFile(Path.Combine(AppContext.BaseDirectory, $"appsettings.{environment.environmentName}.json), optional: false)"
.AddJsonFile(Path.Combine(AppContext.BaseDirectory, "connectionsettings.json"), optional: false)
.Build();
}
}
Then you can go ahead and access them kinda like before
configuration.GetConnectionString("MyConnectionString");
and your json file must look like that
"ConnectionStrings": {
"MyConnectionString": "some-connectionstring-value"
}