Difference between app.config and appsettings.json
Asked Answered
C

4

7

I am trying to create a console application using .NET framework 4.7.2. I see an app.config file by default present in the project.

App.config is used to store configuration details of project. Just wanted to know the difference between app.config and appsettings.json

Consistence answered 9/2, 2023 at 7:47 Comment(3)
Since there is already two answers, I'll add this as a comment. app.config was part of a layering concept, where there was also a user and and a machine config. But since deployments are getting more and more atomic (containerization), there is no need for such things in many cases and a much more lightweight format like appsettings.json can be used.Detta
@SebastianEdelmeier I think this comment is worth an answer. It means that by using appsettings.json I know faster what effective configuration is actually used and I don't have to check for machine.config and user.config, right? Because otherwise json settings has just couple "barely advantages" and big disadvantage of losing compile-time validation.Iluminadailwain
This is partly true. Both XML and JSON support schemas to enable validation. (I think you will find an example here: dev.to/karenpayneoregon/…). I haven't tried this before, but I'm pretty sure you can add a build step that performs validation. Maybe this also helps: newtonsoft.com/json/help/html/JsonSchema.htmDetta
P
10

app.config is used to store configuration details for a .NET Framework application, and it's a traditional way to store configuration data in XML format. However, in recent times, there's a trend towards using appsettings.json files instead of app.config for storing configuration data in .NET applications.

The main difference between app.config and appsettings.json is the format of the data they store. app.config uses XML format, while appsettings.json uses JSON format.

Another difference is that app.config is specific to .NET Framework, while appsettings.json is used in .NET Core applications. You also get web.config, which is used specifically for a .NET Framework Web based application.

In your case I would go with what the framework provided and use the app.config file.

Petronilapetronilla answered 9/2, 2023 at 8:4 Comment(1)
You will find that System.Configuration.ConfigurationManager is still bundled with .NET Core, so you can still use app.config without having to add any new packages or dependencies.Salvage
U
4

Here are some key differences

  • app.config is used usually in .NET Framework applications, while appsettings.json is commonly used in .NET Core applications.
  • app.config uses XML format to store the configuration, appsettings.json uses JSON format. JSON is easier to read and write compared to XML.
  • appsettings.json makes it easier to manage specific configurations for specific environments (you can have different JSON files for each environment appsettings.Development.json, appsettings.Production.json).
Unkindly answered 9/2, 2023 at 7:55 Comment(2)
“JSON is easier” is rather subjective. XML syntax is more verbose, but I don’t think it any harder.Amyamyas
Another difference is that JSON doesn't support comments. This is a major drawback when using it for configuration information. It's frequently useful to note what the valid values are for config settings as well as notes on dependencies e.g. If Foo is set to X, then Bar needs to contain a valid date. JSON is great for a lot of things - config information isn't one of them.Dutcher
F
3

"app.config" is typically used in older .NET Framework applications - format with XML, while "appsettings.json" is used in .NET Core applications - With json format

Fireboard answered 9/2, 2023 at 10:13 Comment(0)
C
0

From a programmer's perspective, the difference also is that the app.config XML is not edited by hand but configured using a class called Settings code which is configured via UI in Visual Studio by opening the Settings.settings file. The key-value pairs are immediately added as properties to the Settings class and immediately serialized to XML so everything is compile-time checked in real time. A bit similar to how Windows Forms designer works. The keys and values can then be accessed in code via public properties of the Settings class.

While appsettings.json in ASP.NET Core is plain text (JSON) file without any built-in validation when edited. This makes it much more prone to mistakes (typos) and also divergence in the config file vs code (no class with no public properties, but .GetSection(string) compared to app.config in .NET Framework.

Crepitate answered 24/4, 2023 at 22:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.