Access from class library to appsetting.json in Asp.net-core

Parameswar Rao picture Parameswar Rao · Jul 15, 2016 · Viewed 54.7k times · Source

I am trying to access appsetting.json file from a class library. So far the solution that I found is to create a configuration class implementing interface IConfiguration from Microsoft.Extensions.Configuration and add the json file to class and read from the same.

var configuration = new Configuration();
configuration.AddJsonFile("appsetting.json");
var connectionString= configuration.Get("connectionString");

This seems to be bad option as we have to add the json file each time we have to access the appsetting configuration. Dont we have any alternative like ConfigurationManager in ASP.NET.

Answer

Stephen P. picture Stephen P. · Sep 17, 2016

I know an answer has already been accepted, but this questions is a top hit on Google and OPs question is about class libraries and not an ASP.NET Web App or a WebApi which is what the accepted answer uses.

IMO, class libraries should not use application settings and should be agnostic to such settings. If you need application settings in your class library, then you should provide those settings from your consumer. You can see an example of this On This SO Question