20

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?

3 Answers 3

17

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Using SetBasePath is necessary in my case var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(filepath);
Note that Directory.GetCurrentDirectory() will just return the windows folder usually... Better to use AppDomain.CurrentDomain.BaseDirectory to get the actual application path (more info here - stackoverflow.com/questions/6041332/…)
Microsoft.Extensions.Configuration is not available for .net framework 4.5
10

Nicely done "Johann Medina", but you forgot to explain a little bit your solution in order people can understand better the idea. I have done it the same way, but here it's the full explanation in order to use it this way...

appsetting.JSON example:

{ "Email": { "Host": "smtp.office365.com", "Port": "587", "From": "[email protected]", "Username": "[email protected]", "Password": "IsAAbbUaa22YTHayTJKv44Zfl18BClvpAC33Dn5p4s=" } } 

POCO class example:

namespace AppSettingsTest { public class AppSettings { public Email Email { get; set; } } public class Email { public string Host { get; set; } public int Port { get; set; } public string From { get; set; } public string Username { get; set; } public string Password { get; set; } } } 

Getting appsetting.JSON content example:

 using (var reader = new StreamReader(Directory.GetCurrentDirectory() + "/appsettings.json")) { var appSettings = JsonConvert.DeserializeObject<AppSettings>(reader.ReadToEnd()); } 

NOTE: Just make sure to set "Copy always" in the property option "Copy to Output Directory" to the JSON file.

Happy Coding!

Comments

1

what about this?

using (var reader = new StreamReader(Directory.GetCurrentDirectory() + "/appsettings.json")) Settings = JsonConvert.DeserializeObject<Settings>(reader.ReadToEnd()); 

1 Comment

What about it??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.