Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access appsettings.json from .NET 4.5.2 project

I have two projects, a 1.1.0 ASP.NET Core project and a reference to a 4.5.2 project.

I want to get values from the appsettings.json file to my 4.5.2 project. The appsettings.json file is in the core project.

Tried this from my 4.5.2 project with no luck:

var mailServer = ConfigurationManager.AppSettings["MailServer"];

How can I access the values from my 4.5.2 project?

like image 320
EResman Avatar asked Sep 05 '17 11:09

EResman


1 Answers

ConfigurationManager works with XML-based configuration files that have specific format and doesn't know how to read the custom JSON file.

Cause you use appsettings.json for the ASP.NET Core project as well, you may add dependencies to Microsoft.Extensions.Configuration and Microsoft.Extensions.Configuration.Json packages and read settings using ".NET Core approach":

var builder = new ConfigurationBuilder()
              .AddJsonFile(@"<path to appsettings.json>");

var configuration = builder.Build();
//  configuration["MailServer"]

Of course, as appsettings.json is a simple JSON file, you may always directly deserialize using any JSON provider.

like image 138
Set Avatar answered Oct 02 '22 15:10

Set