0

I've an Asp.Net Core application. When we first launch it, the code check for some variable in it, and if missing, it displays a wizard, which allow our users to configure some stuff. Those configured element are then stored in this appsettings.json(could be SMTP settings, mongo db settings, ...) and the app is restarted to take those into account.

This part work fine. The issue is that this file gets commited by every developer on every change, and some things are referencing things that are local(could be the schema version of our database, or some folder). We tried to put this file in the git ignore list, but since there are some settings(like log level) that are present(and should stay present), the file needs to be presents and its updates are tracked.

So I'm wondering: Is there a way to have some sort of "template" of appsettings.json, like initial.appsettings.json, and when the Asp.Net Core app starts, check if the file is missing, copy it and restart if needed?

Like this we can have this initial.appsettings.json being tracked, and ignore the appsettings.json in our git repository?

J4N
  • 19,480
  • 39
  • 187
  • 340
  • https://stackoverflow.com/a/60961168/8125309 - This might help you out a bit :) – Tachyon Feb 15 '23 at 06:55
  • @Tachyon I've checked your link, I'm not sure it helps, because those properties have to be stored and persisted also in production builds. Those are populated by a wizard when starting the web app for the first time – J4N Feb 15 '23 at 11:49

1 Answers1

1

After test, the coon value will be overriden by the settings in connectionstrings.json file. You can find it below.

enter image description here

enter image description here


Splitting the files in appsettings.json is a good option for your current situation.

You can divide the configuration in the appsettings.json file into connectionstrings.json, and logging.json according to your requirements.

var builder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    .AddJsonFile("connectionstrings.json", optional: true, reloadOnChange: true)
    .AddJsonFile("logging.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables();

Load it at startup with the following code. Then you are ignoring which specific files according to your needs.

Jason Pan
  • 15,263
  • 1
  • 14
  • 29
  • I was not aware of this option(and especially the reloadOnChange, that's very interesting. What if `appsettings.json` and `connectionstrings.json` declare the same option, will this override the previous one? I'm thinking of having a file with the "default" values, and then a new file that override the default values with the one that have been configured. That would be awesome. – J4N Feb 16 '23 at 07:05
  • If you have plan to deploy your app in azure app service, the configurations could be overwriten. – Jason Pan Feb 16 '23 at 07:21
  • Sorry, I probably mispoke, I was wondering that if in the two files you provide with the AddJsonFile, if the seconds contains some settings already contained in the first one, is this overriding the first one? That would allow me to have something like `new ConfigurationBuilder().AddJsonFile("defaultSettings.json", ...).AddJsonFile("userSettings.json");` – J4N Feb 16 '23 at 11:46
  • is this overriding the first one? @J4N, yes, I test it, you can check my updated answer. – Jason Pan Feb 17 '23 at 08:01