c# - ConfigurationManager.AppSettings return The given key was not present in the dictionary 错误
问题描述
我在 ConfigurationManager.AppSettings 上遇到错误,错误是 The give key was not present in the dictionary
代码:
string userPass = ConfigurationManager.AppSettings["dev_pass"];
解决方案
如果您使用的是经典 ASP.NET,请确保将该密钥添加到web.config
的应用程序设置中...
<appSettings>
<add key="dev_pass" value="THE_VALUE" />
</appSettings>
如果您使用的是 ASP.NET Core,请确保您遵循 ASP.NET Core 的指南从appsettings.json
. 假设您appsettings.json
的配置如下...
{
"AppSettings": {
"dev_pass": "THE VALUE"
}
}
然后创建这个类...
public class AppSettings
{
public string dev_pass{ get; set; }
}
然后将其配置为服务...
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}
推荐阅读
- angular - 提交后如何禁用Angular反应表单中的按钮
- javascript - 如何使用 babel 预设环境保留动态导入语句?
- xampp - 如何更改默认 XAMPP 文件浏览器的布局和/或 css?
- java - 单击充当类似按钮的 ImageView 会选择多个 ImageView 而不是仅我选择的一个
- laravel - 存储符号链接后Xdebug无法正常工作 - Laravel
- python - 在 pytest 配置文件中排除测试
- google-apps-script - 使用 App Script 将多个 Google 表格文件附加到一个 Google 表格中
- mysql - 通过 ODBC 查询依赖视图
- flutter - 是否可以仅使 DataTable 小部件的行可滚动?
- mule - Mule 4 - Anypoint MQ 重试耗尽异常和死信队列