ASP.NET Core was the first to introduce the replacement for the traditional XML-based configuration in .NET via App.config
and Web.config
, which we used since… well, always. The modern configuration system Microsoft.Extensions.Configuration.*
is easily extensible and flexible. Even better – as there is no dependency on the ASP.NET Core infrastructure, it can easily be used in any .NET app! In this article, we will see how to set up a UWP app to use JSON-based configuration through appsettings.json
file.
Installing the NuGets
All of the modern configuration libraries can be installed easily from NuGet package manager. For our purposes, we need the following packages:
The 3.x versions of these packages support .NET Standard 2.0, so they are easily portable.
Setting up the configuration
To contain all the configuration logic in one place, we create an AppConfig
class. In its constructor, we need to set up the configuration pipeline. As the JSON file is deployed with the application package, we set the base path for file searching to Package.Current.InstalledLocation.Path
. Then we add appsettings.json
to the pipeline:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private readonly IConfigurationRoot _configurationRoot; | |
public AppConfig() | |
{ | |
var builder = new ConfigurationBuilder() | |
.SetBasePath(Package.Current.InstalledLocation.Path) | |
.AddJsonFile("appsettings.json", optional: false); | |
_configurationRoot = builder.Build(); | |
} |
Now we need to add a appsettings.json
file to our project. We will have a single example setting there:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"example": { | |
"message" : "Hello from appsettings.json!" | |
} | |
} |
Important note – make sure the appsettings.json
file has its Build Action set to Content
and its Copy to Output Directory to Copy Always
in the properties, so that it is indeed deployed with the application. We can do so by selecting the file and adjusting its options in the Properties window:

Retrieving configuration sections
To make it easy to retrieve strongly-typed configuration sections, we prepare a simple helper method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private T GetSection<T>(string key) => _configurationRoot.GetSection(key).Get<T>(); |
Now we cover our “example” section with a simple POCO class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Example | |
{ | |
public string Message { get; set; } | |
} |
And create a property to retrieve this section:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Example Example => GetSection<Example>(nameof(Example)); |
That’s all there is to it! Now we can simply use the AppConfig
class, anywhere we need it:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = new AppConfig(); | |
var contentDialog = new ContentDialog() | |
{ | |
Content = config.Example.Message, | |
CloseButtonText = "OK" | |
}; | |
await contentDialog.ShowAsync(); |
Lo and behold, our message is shown!

If you are using a MVVM framework with dependency injection, the best way to use configuration is to extract an interface from the AppConfig
class and inject the instance as a singleton into your view models instead of instantiating it manually.
Bonus tip
A great thing about the modern configuration infrastructure is that we could also have a appsettings.production.json
file, which would not be checked-in the source control and we could include it in our project only for Release builds (by making it conditional in csproj
):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ItemGroup Condition="'$(Configuration)' == 'Release'"> | |
<Content Include="appsettings.production.json"> | |
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | |
</Content> | |
</ItemGroup> |
The pipeline would then be configured as follows:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = new ConfigurationBuilder() | |
.SetBasePath(Package.Current.InstalledLocation.Path) | |
.AddJsonFile("appsettings.json", optional: false) | |
.AddJsonFile("appsettings.production.json", optional: true); |
Unfortunately, UWP projects can’t use the new SDK-style csproj
format yet and Visual Studio has issues properly showing/hiding the conditional file in Solution Explorer for the classic csproj
format. Even though this is inconvenient, it still does work as intended!
Source code
Example source code for this article is available on my GitHub.
Summary
It is straightforward to include Microsoft.Extensions.Configuration
into a UWP project, or in general, any .NET project. We have seen how to use it with JSON configuration files, but it is easily extensible and there are many configuration providers already available for you outside the box in the form of NuGet packages.
Your solution is both elegant and very helpful. However, I found that installing a side-loaded app (built using the wizard as a Release) through the appinstaller file does not copy the appsettings.production.json to the install folder. Installing via the appxbundle does. I would prefer to use the former. I have found little information that states the reasoning behind this. Do you know why this would be the case? Thanks again for this!
SetBasePath was super helpful Martin. Thanks for your great and helpful articles!
Hello Martin,
Thank you for this nice, simple approach for implementing app settings. I was able to use this in my WPF(.NetCore) app.
Hello, thanks for your post. It helped me a lot.
I want the user to set the settings in my app. Is there a “best practice” way to serialize and save them back to appsettings.json?