.NET Core 2.0 处理多个项目时的 appsettings.json 文件位置 [英] .NET Core 2.0 appsettings.json file location when dealing with multiple projects
问题描述
我正在开发一个由多个项目组成的 .NET Core 2.1.103 WebAPI 应用程序.每个项目都需要有自己的 appsettings.json 文件.每个项目都位于主解决方案"文件夹中的自己的子目录中.这是目录结构的简化版本,其中包含我的问题的相关目录:
I'm working on a .NET Core 2.1.103 WebAPI application that consists of multiple projects. Each project needs to have its own appsettings.json file. Each project is in its own subdirectory within a master "solution" folder. Here's a simplified version of the directory structure with the relevant directories for my question:
c:\code\my_big_solution
c:\code\my_big_solution\project1
c:\code\my_big_solution\project1\bin\Debug
c:\code\my_big_solution\project2
c:\code\my_big_solution\project2\bin\Debug
c:\code\my_big_solution\project3
c:\code\my_big_solution\project3\bin\Debug
我很难弄清楚每个 appsettings.json 文件必须放在何处以及如何读取它们.我通过阅读其他帖子假设我应该自动将每个项目的 appsettings.json 文件复制到其关联的 bin\Debug
目录.如果是这种情况,我不确定如何使用 Startup 构造函数.我的理解是,这是从 appsettings.json 文件中读取值的 .NET Core 2.0 方式:
I am having difficulty figuring out where each appsettings.json file must go and how to read them in. I assume from reading other posts that I should automatically copy each project's appsettings.json file to its associated bin\Debug
directory. If that's the case, I'm not sure how to work with the Startup constructor. My understanding is that this is the .NET Core 2.0 way of reading a value from an appsettings.json file:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
string myString = Configuration["blah:blah:blah"];
configuration
似乎是从 c:\code\my_big_solution
获取其 appsettings.json 文件,而不是 c:\code\my_big_solution\project1\bin\Debug
或我想从中提取的任何项目的 Debug
目录.
configuration
appears to be getting its appsettings.json file from c:\code\my_big_solution
, not c:\code\my_big_solution\project1\bin\Debug
or whatever project's Debug
directory I want to pull from.
我不知道如何从所需的目录中提取,除了使用 .NET Core 1.x 类型的方法,这是通过更改构造函数以便它传入 IHostingEnvironment
并使用 ConstructorBuilder
:
I can't figure out how to pull from the desired directory, other than doing a .NET Core 1.x sort of way of doing it, and that's by changing the constructor so it's passing in an IHostingEnvironment
and using a ConstructorBuilder
:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath ) // This line is wrong, but the correct path would be set here
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
string myString = Configuration["blah:blah:blah"];
虽然第二个解决方案可行,但这让我感到困扰,因为我认为在 .NET Core 2.0 中有更正确"的方法来做到这一点.有吗?
Although the second solution would work, this bugs me because I'm thinking there is a more "correct" way to do this in .NET Core 2.0. Is there?
推荐答案
我知道发生了什么.在launch.json 文件中,我没有为cwd 参数设置正确的路径.这就是 Startup() 在错误的位置寻找 appsettings.json 文件的原因.
I figured out what was going on. In the launch.json file, I didn't have the correct path set for the cwd parameter. That's why Startup() was looking for the appsettings.json file in the wrong place.
这篇关于.NET Core 2.0 处理多个项目时的 appsettings.json 文件位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!