Search code examples
c#visual-studioasp.net-coreconfiguration.net-6.0

How to read the configuration not in the current project?


As we all know, for Web API projects created based on .NET 6.0, Visual Studio will automatically generate the appsettings.json configuration file, which can be read through the constructor-injected Configuration in the current project (dll).

So how to read the configuration that is not in the current project?


Solution

  • So how to read the configuration not in the current project

    If you can get the relative path (Full Path), you can try the below way(In ApiAppseting project to get the Application2 appsettings.json):

    var path = @"C:\Users\Administrator\source\repos\ApiAppseting\Application2\appsettings.json";
    var configuration = new ConfigurationBuilder()
      .AddJsonFile(path)
      .Build();
    var precision = configuration.GetValue<string>("aa");
    

    Result: enter image description here