This is a migrated thread and some comments may be shown as answers.

ConnectionString in Asp.Net Core

1 Answer 379 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Henrique Duarte
Top achievements
Rank 1
Veteran
Henrique Duarte asked on 16 Jan 2020, 08:30 AM

Can I use the ConnectionStrings in appsettings.Development.json/appsettings.Production.json?

Everytime I try todo so, the report  gives me an exception, but if if move the same exact ConnectionString to appsettings.json file, the report runs.

1 Answer, 1 is accepted

Sort by
0
Todor
Telerik team
answered on 20 Jan 2020, 02:33 PM

Hello Henrique,

You may check our updated articles on .NET Core applications:

The following code assures that the project will use the default configuration file, and will respect the appSettings.{environment}.json file :

// otherwise instantiate the default builder which will use the default appSettings.json and appSettings.{environment}.json files.
var defaultBuilder = Microsoft.AspNetCore.WebHost.CreateDefaultBuilder()
    .Build();
return defaultBuilder.Services.GetService<IConfiguration>();

 

When the configuration file name is provided explicitly the connection string will be searched for in the exact file path specified. In this case, you may set the name for the configuration file conditionally, based on the environment, for example:

if (environment.IsDevelopment())
{
    configFileName = "appsettings.Development.json";
}
else
{
    configFileName = "appsettings.json";
}

var configFilePath = System.IO.Path.Combine(environment.ContentRootPath, configFileName);
return new ConfigurationBuilder()
                .AddJsonFile(configFilePath, true)
                .Build();

Regards,
Todor
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Henrique Duarte
Top achievements
Rank 1
Veteran
Answers by
Todor
Telerik team
Share this question
or