Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 924 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to properly read nested configuration values from config.json in ASP.NET5?

#1
I was following some [examples for ASP.NET 5](

[To see links please register here]

) 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](

[To see links please register here]

) (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](

[To see links please register here]

).

Also, I found similar [SO question](

[To see links please register here]

) but this does not give explanation of why `:` was chosen.


Reply

#2
Peering deep into the bowels of the [JsonConfigurationFileParser][1] source with blame on for the enter/exit methods that look at:

private void VisitJObject(JObject jObject)
{
foreach (var property in jObject.Properties())
{
EnterContext(property.Name);
VisitProperty(property);
ExitContext();
}
}

private void EnterContext(string context)
{
_context.Push(context);
_currentPath = string.Join(":", _context.Reverse());
}

private void ExitContext()
{
_context.Pop();
_currentPath = string.Join(":", _context.Reverse());
}

it seems that the ASP.NET team should leave more illuminating check-in comments :).

My best guess is that there could be data stored in the config.json file that would need to have a `.` in it, whereas `:` would be less common. For instance:

"AppSettings": {
"Site.Title": "Is .NET getting faster?"
},

It's a bad example, but it seems reasonable that they wanted to be as "safe" as possible and use something outside of the norm. If you wanted to store a type's full name, that would also be slightly easier without needing to worry about a stray period.


"AppSettings": {
"ImportantTypeName": "WebApp.CoolStuff.Helpers.AwesomeClass"
},

[1]:

[To see links please register here]

Reply

#3


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"
},
}


Reply

#4
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](

[To see links please register here]

) which binds a configuration to a model (a strong type object). [Here are the tests on GitHub](

[To see links please register here]

) that can serve as example.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through