Lukas
Lukas

Reputation: 2263

What does the "optional" parameter in AddJsonFile do?

I am using AddJsonFile method to add the correct appsettings files to my configuration. The method has an bool optional parameter that decides "whether the file is optional."

What does that mean, exactly? How will the app behave differently if the setting is enabled or disabled? What would be examples of where it needs to be set to true and false?

Upvotes: 3

Views: 3990

Answers (1)

Jeroen Mostert
Jeroen Mostert

Reputation: 28809

All it does is throw an exception on startup if you specify false and the file's not there (which is the default, for the method that doesn't take the parameter). It's entirely up to you when you'd want this, but typically, it helps guard against accidentally deleting or forgetting to copy a file with settings that you couldn't guess and don't all have defaults -- a (documented) configuration file is its own reward. Conversely, optional files would be used to specify overrides (in a read-write location) that need not be present; think shared file with global settings and a per-user override. (For things like websites this is obviously not as relevant, but you could still have an "administrator" config file with defaults and a "deployment specific" file with tunable settings.)

Upvotes: 4

Related Questions