Home > Blockchain >  Hot to access appsettings.json from another project in same solution?
Hot to access appsettings.json from another project in same solution?

Time:03-30

I have a solution with two projects:

  • ASP.NET Core 6 Web API Project
  • Class Library Project

I want to access the appsettings.josn file in the web api project from within the class library.

The api project already has a reference to the class library (Request/Response Models, Services, Handlers...etc).

Adding a reference for the api project to the class library would create a circular reference problem.

What's the best solution for this problem? Do I ditch the 2-project solution structure and place everything under 1 project?

CodePudding user response:

Make a get function that return information that you want from project that is owner of json settings

CodePudding user response:

We do not recommend using the ConfigurationManager Class in the class library to get the value of appsettings.json.

Reason:

enter image description here

Test Method in Controller

public string GetValue()
{
    GetValue g = new GetValue();
    string result = g.fortest(_settings.Value.SmtpHost,_settings.Value.EmailRecipients);
    return result;
}

enter image description here

Test Result

enter image description here

  • Related