[英]ASP.NET Core 2.0 - Configuration[“TestSecret”] always return null
I am starting to work on an ASP.NET Core 2.0 Web Api . 我开始在ASP.NET Core 2.0 Web Api上工作 。 I added 2 secrets to the secrets.json file and am trying to read them in through the Configuration property in my Startup file. 我在secrets.json文件中添加了2个机密,并试图通过Startup文件中的Configuration属性读取它们。 Each time I called I try to get the value from the Configuration
variable, it returns null. 每次我打电话给我时,都尝试从Configuration
变量获取值,它返回null。 An example of how I am reading this from the secrets.json is shown below. 下面显示了我如何从secrets.json读取此示例。
public void ConfigureServices(IServiceCollection services)
{
var secret = Configuration["TestSecret"];
My secrets.json file looks like: 我的secrets.json文件如下所示:
{
"TestSecret": "SecretValue"
}
I have also tried to retrieve the value by using: 我也尝试过使用以下方法检索值:
public void ConfigureServices(IServiceCollection services)
{
IConfigurationSection secret = Configuration.GetSection("TestSecret");
var value = secret.Value;
This returns a section that corresponds to the TestSecret
section, but the value in the IConfigurationSection
is also null. 这将返回与TestSecret
部分相对应的部分,但IConfigurationSection
的值也为null。
I have tried to install the Microsoft.Extensions.Configuration.UserSecrets
NuGet package, but this hasn't helped. 我试图安装Microsoft.Extensions.Configuration.UserSecrets
NuGet程序包,但这没有帮助。 Am I missing a package I need to install or is there an alternate way to retrieve this value? 我是否缺少需要安装的软件包,或者是否有其他方法来检索此值?
If I need to provide any more information to help solve this issue, please ask in the comments. 如果我需要提供更多信息来帮助解决此问题,请在评论中提出。 I will try to provide as much information as possible to help solve this issue. 我将尝试提供尽可能多的信息来帮助解决此问题。
In general you use a file called "appSettings.json" file for storing all json values like that. 通常,您使用一个名为“ appSettings.json”的文件来存储所有类似的json值。 This file doesn't need to be manually added in 2.0. 无需在2.0中手动添加此文件。 Unless there is a specific reason for having multiple json files I would recommend doing this as it allows you to have all your application specific settings in the one place 除非有多个json文件的特定原因,否则我建议您这样做,因为它允许您将所有应用程序特定的设置都放在一个位置
it is possible to manually add a .json file though. 可以手动添加.json文件。 for asp.net Core 2.0 in your Program.cs in the BuildWebHost method BuildWebHost方法中Program.cs中的asp.net Core 2.0
you add in the following 您添加以下内容
.ConfigureAppConfiguration((WebHostBuilderContext, ConfigurationBuilder) =>
{
ConfigurationBuilder
.AddJsonFile("Secrets.json", optional: true);
ConfigurationBuilder.AddEnvironmentVariables();
})
depending on the setup the entire method should look like 根据设置,整个方法应如下所示
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((WebHostBuilderContext, ConfigurationBuilder) =>
{
ConfigurationBuilder
.AddJsonFile("Secrets.json", optional: true);
ConfigurationBuilder.AddEnvironmentVariables();
})
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
To get a value from in your ConfigureServices method you use 要从您的ConfigureServices方法中获取值,请使用
var testSecret = Configuration.GetSection("Secrets")["TestSecret"];
this is how the Secrets.Json file should look 这是Secrets.Json文件的外观
{
"Secrets":
{
"TestSecret": "SecretValue"
}
}
For me it was null because I was playing around with other ASPNETCORE_ENVIRONMENT values and the default logic is apparently dependent on this being "Development"... which makes basic sense given that secrets.json is entirely local dev scenario. 对我来说,它是空的,因为我正在处理其他ASPNETCORE_ENVIRONMENT值,并且默认逻辑显然依赖于此“开发” ...鉴于secrets.json完全是本地开发方案,这具有基本意义。
As a related aside, for us there's mild heartburn that "Development" could mean either cloud runtime dev or raw local dev so we're forced to create yet another key/value to represent local dev because ASPNETCORE_ENVIRONMENT = Development drives specific logic we don't want to lose. 另外,对我们而言,“开发”可能意味着云计算运行时开发人员或原始本地开发人员,这让人感到有点胃灼热,因此我们被迫创建另一个代表本地开发人员的键/值,因为ASPNETCORE_ENVIRONMENT =开发驱动着我们所不愿做的特定逻辑不想输。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.