gpt4 book ai didi

c# - 配置绑定(bind)不起作用

转载 作者:行者123 更新时间:2023-11-30 13:44:01 26 4
gpt4 key购买 nike

我有以下设置:

{
"AppSettings": {
"ConnectionString": "mongodb://localhost:27017",
"Database": "local",
"ValidOrigins": [ "http://localhost:61229" ]
},
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}

我做绑定(bind):

    services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

我有以下设置文件:


public class AppSettings
{
public string ConnectionString = "";
public string Database = "";

public List<string> ValidOrigins { get; set; }
}

进行绑定(bind):

    AppSettings settings = new AppSettings();
Configuration.GetSection("AppSettings").Bind(settings);

settings.ValidOrigins 没问题,但是 ConnectionStringDatabase 都是 null。我做错了什么?

最佳答案

Binder 只会绑定(bind)属性,不会绑定(bind)字段。尝试对 ConnectionStringDatabase 使用属性而不是字段。

public string ConnectionString { get; set; }

public string Database { get; set; }

关于c# - 配置绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46426386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com