Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly read nested configuration values from config.json in ASP.NET5?

I was following some examples for ASP.NET 5 and I got stumbled with how to properly read "nested" configuration values (if that is the proper term).

Here is relevant portion of config.json:

{
    "ApplicationName" : "OwNextApp",
    "AppSettings": {
        "SiteTitle": "OwNext"
    },
}

And relevant portion of HomeController.cs:

public IActionResult About()
{
    var appNestedNameFailed = _config.Get("AppSettings.SiteTitle");
    var appNestedNameSuccess = _config.Get("AppSettings:SiteTitle");
    var appName = _config.Get("ApplicationName");
    ViewBag.Message = string.Format(@"Your 
        APP NAME: {0};
        APP NESTED NAME FAILED: {1}; 
        APP NESTED NAME SUCCESS: {2}", 
            appName, appNestedNameFailed, appNestedNameSuccess);

    return View();
}

Value for appNestedNameFailed is empty (my initial try before research). And appNestedNameSuccess has value; after I did research and found in tests for Configuration (relevant code shown):

// Assert
Assert.Equal("IniValue1", config.Get("IniKey1"));
Assert.Equal("IniValue2", config.Get("IniKey2:IniKey3"));

Can someone explain why is this the case? Why would it make sense to use : over .? From my interaction with JSON data usually . notation works fine, e.g. How to access nested json data.

Also, I found similar SO question but this does not give explanation of why : was chosen.

like image 727
CrnaStena Avatar asked May 20 '15 02:05

CrnaStena


People also ask

How read config file in ASP.NET Core?

To read configuration values in ASP.NET Core, you need to follow the Options pattern. To implement it, define a configuration class matching the values you want to read from the appsetttings. json file and use the default dependency container to inject the read values.

Is ASP.NET Core configuration system is json based?

Asp.net Core uses Project. JSON file for storing all project level configuration settings. The Project. json file stores configuration information in JSON format.

How do I access Appsettings json in controller?

Using IConfiguration The IConfiguration is available in the dependency injection (DI) container, so you can directly access JSON properties by simply injecting IConfiguration in the constructor of a controller or class. It represents a set of key/value application configuration properties.

How do you inject IConfiguration in net 6?

You could add the IConfiguration instance to the service collection as a singleton object in ConfigureServices : public void ConfigureServices(IServiceCollection service) { services. AddSingleton<IConfiguration>(Configuration); //... }


2 Answers

That's the convention that we decided upon when we first created the configuration model. We started with json in mind and : is the delimiter there.

Anyways, if you don't want to worry about those conventions, I recommend using the ConfigurationBinder which binds a configuration to a model (a strong type object). Here are the tests on GitHub that can serve as example.

like image 136
Victor Hurdugaci Avatar answered Sep 18 '22 15:09

Victor Hurdugaci


using Microsoft.Extensions.Configuration;
using System.IO;

IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

var connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnection");

// or

var connectionString2= configuration.GetSection("ConnectionStrings").GetSection("DefaultConnection").Value;  

appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "myconnection"
  },
}
like image 34
Roohi Avatar answered Sep 20 '22 15:09

Roohi