大火。 ASP.NET托管AppSettings.json
在 Visual Studio 中从项目的服务器端运行我的项目时,我无法访问 appsettings.{env}.json。特定 env json 文件中的所有键都会在 builder.Configuration 对象上返回 null。它们仅加载到主 appsettings.json 文件中。对于特定的 json 文件,project.server 和 project.client 中的调试环境是相同的(非常确定客户端环境不重要)。没有看到其他人遇到这个问题,我错过了什么? 谢谢
[编辑]
.NET 6.0
以下是在 Project.Server 和 Project.Client 调试启动配置文件中设置的
ASPNETCORE_ENVIRONMENT=Project.Server 中的 Development
Program.cs Project.Server 中的
IConfiguration config = builder.Configuration;
var serverVersion = new MySqlServerVersion(new Version(10, 7));
builder.Services.AddDbContext<ProjectMariaDbContext>(
dbContextOptions => dbContextOptions
.UseMySql(config["ConnectionStrings:MariaDb"], serverVersion) );
appsettings.Development.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
},
"ConnectionStrings": {
"MariaDb": "server=maria; port=3306; database=project; user=root; password=testing;"
}
}
}
在添加 dbcontext 行的断点处立即立即窗口中进行测试
config["ConnectionStrings:MariaDb"]
null
另请注意,我正在从 VS 中的 Project.Server 配置运行该项目
When running my project from the server side of the project in visual studio I am unable to access appsettings.{env}.json. All keys in the specific env json file return null on the builder.Configuration object. They only load in the main appsettings.json file. The environment for debugging is the same in the project.server and project.client (Pretty sure the client env shouldn't matter) for the particular json file. Haven't seen anybody else with this problem, what am I missing?
Thanks
[EDIT]
.NET 6.0
Below is set in both Project.Server and Project.Client Debug launch profiles
ASPNETCORE_ENVIRONMENT=Development
Program.cs in Project.Server
IConfiguration config = builder.Configuration;
var serverVersion = new MySqlServerVersion(new Version(10, 7));
builder.Services.AddDbContext<ProjectMariaDbContext>(
dbContextOptions => dbContextOptions
.UseMySql(config["ConnectionStrings:MariaDb"], serverVersion) );
appsettings.Development.json in Project.Server
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
},
"ConnectionStrings": {
"MariaDb": "server=maria; port=3306; database=project; user=root; password=testing;"
}
}
}
Testing in the immediate Immediate window at a break point on adding dbcontext line
config["ConnectionStrings:MariaDb"]
null
Also to note I am running the project from the Project.Server configuration in VS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论