The work in replacing the UI layer for DasBlog has forced me to create rather large appsettings.json file, it has taken the place of an XML file for storing static site wide data. You can pull the information from the appsettings.json file with dependency injection, it is configured during startup in the Startup.cs file:

var builder = new ConfigurationBuilder()
     .SetBasePath(env.ContentRootPath)
     .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)

In the ConfigureServices method you can pull the settings section and assign it to a class you create called DasBlogSettings:

services.Configure<DasBlogSettings>(Configuration.GetSection("DasBlogSettings"));

So I am struggling with keeping up with the changes to DasBlogSettings class, for every property I have I add the json settings file I have to manually reflect it in the DasBlogSettings class, ugh!

Well actually you can use Visual Studio to generate a class directly from any JSON formatted snippet, and the steps are straightforward.

  • Open your appsettings.json file, for example, and copy the section your want to serialize
  • Create a new cs file and delete the class stub it creates for you.
  • Click on the Edit menu and select Paste Special>Paste JSON As Classes

Paste_JSON_As_Classes

You can do this for XML as well! This will save me hours of error prone work, apparently this been around for a while but I was completely unaware of it.



Comment Section

Comments are closed.