asp.net-core - 访问我的 asp.net 核心控制台应用程序中的 appsettings.json 自定义部分
问题描述
appsettings.json
我的Asp.NET 核心控制台应用程序中有以下内容:-
{
"ConnectionStrings": {
"ConnectionString": "Server=localhost;Database=ServiceDesk;Trusted_Connection=True"
},
"SP": {
"SiteURL": "https://***.sharepoint.com/"
}
}
现在我想访问SiteURL
,我厌倦了以下但它不起作用:-
var section = config.GetSection("SP");
var ClientConfig = section.GetChildren();
string siteUrl = ClientConfig.ToString();
解决方案
创建与设置匹配的模型以存储数据
public class SharePointOptions {
public string SiteURL { get; set; }
}
然后从配置绑定到该部分
SharePointOptions options = config.GetSection("SP").Get<SharePointOptions>();
string siteUrl = options.SiteURL;
ASP.NET Core 中的参考配置
ConfigurationBinder.Get<T>
绑定并返回指定的类型。ConfigurationBinder.Get<T>
可能比使用更方便ConfigurationBinder.Bind
。
推荐阅读
- javascript - 使用 swig 创建 C++ 到 javascript 的包装器时生成错误
- asp.net-core - AspNet Core 定位导航属性
- sql-server - 同一台机器上的 SQL Server 2016 Express 版和开发者版
- symfony - Monolog vs worker vs supervisord
- r - 如何在每行的字符串之前和之后添加一个空格
- swift - Swift:条形图内容未水平居中
- php - 我如何通过在 php OOP 中使用 foreach 循环动态获取 jquery 选项卡的选项卡 ID
- python - 有没有办法在循环中处理具有相似名称(数字差异)的字典中的文件?
- pandas - 使用 np.select 来自另一列的数据
- python-3.x - 如何从用户窗口隐藏 Hangout-chat-bot 未响应消息?