Brijesh Ray
Brijesh Ray

Reputation: 1303

Difference between app.config and appsettings.json

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

Upvotes: 8

Views: 16035

Answers (4)

user3625699
user3625699

Reputation: 164

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.

Upvotes: 0

Huynh Son
Huynh Son

Reputation: 99

"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

Upvotes: 3

Mervyn Ludick
Mervyn Ludick

Reputation: 349

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.

Upvotes: 13

Gor Grigoryan
Gor Grigoryan

Reputation: 362

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).

Upvotes: 4

Related Questions