ASP.NET Core: appsettings.json

Using AppsSettings.Json to Store/Retrieve data

In this article I’ll demonstrate how to use Microsoft’s replacement configuration file to web.config, appsettins.json.

Similar to all .json files, this change means much easier config file for projects due to its simple attribute–value pairs and arrays format, over the highly proprietary web.config file format, which changes often with each new release.

As you can see in the previous example, at the beginning of a project, you are presented with minimal settings. Again, notice the much simplified format compared to the web.config.

To add a new setting, simply add a new AppsSettings.Json attribute–value pair like in this example.

Next, I’ll create a new test page, which also comes with its own page model.

I need to configure the page model with a Message variable to display on the page.

I need to configure the page model with a Message variable to display on the page.
Also, I need to update the page model with the ability to access appsettings.json by giving it a constructor, which receives a variable of IConfiguration. By creating a private IConfiguration variable, the rest of the page model may use it.
Finally, I’ll update OnGet() which responds to http request to access appsettings.json to get the Message attribute.

Now, I’ll update the razor page to display the page model property initialized by its constructor.

As you can see, my Test page now displays the string stored in the appsettings.json attribute.