ConnectionString in Asp.Net Core

2 posts, 0 answers
  1. Henrique Duarte
    Henrique Duarte avatar
    106 posts
    Member since:
    Oct 2003

    Posted 16 Jan Link to this post

    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.

  2. Todor
    Admin
    Todor avatar
    453 posts

    Posted 20 Jan Link to this post

    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
Back to Top