Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional appsettings.local.json in (new format) visual studio project

My app uses appsettings.json for some settings. If appsettings.local.json is present, that should override appsettings.json for whatever settings it contains. So far, no problem.

But I use git for version control. Obviously, I don't want other users pulling down my local settings. So I git ignore appsettings.json.

Furthermore, there are a lot of projects in the solution. They share the same settings. So there is an appsettings.json at the solution level, and all the projects include it as a link.

Still fine, except for one thing. In order to be usable, I have to copy appsettings.local.json over to the output directory. But it shouldn't be in version control. So if someone clones the solution fresh they won't have it. That ought to be fine, but it isn't. VS. says "this file should be linked, but where the heck is it?" build error.

How can I deal with that?

like image 922
William Jockusch Avatar asked May 29 '17 19:05

William Jockusch


People also ask

How do I add Appsettings json to Visual Studio?

Add Json File After adding the file, right click on appsettings. json and select properties. Then set “Copy to Ouptut Directory” option to Copy Always. Add few settings to json file, so that you can verify that those settings are loaded.

What is difference between Appsettings json and Appsettings development json?

NET Core and as far as I see from my search on the web, appsettings.Development. json is used for development config while developing the app and appsettings. Production. json is used on the published app in production server.

Where is launchSettings json Visual Studio?

In the Properties folder in an ASP.NET Core project, you can find the launchSettings. json file, which contains settings that control how your web app is started on your development machine. For detailed information on how this file is used in ASP.NET development, see Use multiple environments in ASP.NET Core.

Can we have multiple Appsettings json?

Of course, we can add and use multiple appsettings. json files in ASP.NET Core project. To configure and read data from your custom json files, you can refer to the following code snippet.


2 Answers

With v2 this is dead simple.

  1. Add an appsettings.local.json to your project (it should nest itself below the main appsettings.json file).
  2. Add appsettings.local.json to your .gitignore
  3. In your startup.cs within the constructor do the following:

    public class Startup
    {
        public IConfigurationRoot Configuration { get; }
    
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) //load base settings
                .AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true) //load local settings
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) //load environment settings
                .AddEnvironmentVariables();
    
            Configuration = builder.Build();
        }
    
        /*
         * rest of your startup.cs
         */
    }
    
like image 149
pim Avatar answered Sep 19 '22 11:09

pim


For .Net Core >2.1 you can simply chain the extension method ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate) to your WebHost. Here is an example:

WebHost.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        config.AddJsonFile(
            "appsettings.Local.json",
             optional: true,
             reloadOnChange: true);
    })
    // ...

And of course ignore the appsettings.Local.json in your .gitignore.

like image 41
othman.Da Avatar answered Sep 21 '22 11:09

othman.Da