【问题标题】:Read appsettings.json - Fields remain null阅读 appsettings.json - 字段保持为空
【发布时间】:2019-06-04 11:34:02
【问题描述】:

认为我的 startup.cs 有问题,因为我没有从我的 <IOption> config 获得任何值

所以.. 我们有我们的 appsettings.json

"Config": {
    "ApplicationName": "some name",
    "ConnectionString": "someconstring",
    "Version": "1.0.0"
  },

这里有我们的模型

public class Config
    {   
        public string ApplicationName { get; set; }
        public string ConnectionString { get; set; }
        public string Version { get; set; }
    }

startup.cs

 public Startup(IConfiguration configuration)

    {
        Configuration = configuration;
    }



public static IConfiguration Configuration { get; set; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        // Add functionality to inject IOptions<T>
        services.AddOptions();

        // Add our Config object so it can be injected
        services.Configure<Config>(Configuration);
    }

然后在我们的控制器中,我尝试加载这些数据,但不幸的是它们仍然是空的。

   private IOptions<Config> config;
            public CompaniesController(IOptions<Config> config)
            {
                this.config = config;
            }

我尝试用类似的东西更改 startup.cs

 services.Configure<Config>(options =>
            {
                options.ConnectionString = Configuration.GetSection("Config:ConnectionString").Value;
            });

但这似乎不起作用。

资源我一直在使用:

https://dzone.com/articles/dynamic-connection-string-in-net-core

https://stackoverflow.com/questions/31453495/how-to-read-appsettings-values-from-json-file-in-asp-net-core

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.2

但显然我在这里遗漏了一个关键点。

编辑:我使用的是 ASP.Net Core 2.0

编辑2:

Program.cs

public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }

整个 Appsettings.json 文件

{
  "Config": {
    "ApplicationName": "somename",
    "ConnectionString": "someconstring",
    "Version": "1.0.0"
  },
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

编辑 3: 在我的前端应用程序中,我像这样导入 API。

【问题讨论】:

  • 我建议您从appsettings.json 文件中删除Config 顶级属性。它没有提供任何价值,因为 appsettings 已经是“配置”,它应该使 services.Configure&lt;Config&gt;(Configuration) 工作。

标签: c# asp.net json asp.net-core


【解决方案1】:
services.Configure<Config>(Configuration);

此行未达到预期结果,因为您要查找的 JSON 属性嵌套在 appsettings.json 文件中的 Config 属性下。要按预期加载这些值,请使用GetSection 获取Config 部分并将that 传递给Configure&lt;TOptions&gt; 方法:

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

【讨论】:

    【解决方案2】:
         services.Configure<Config>(options =>
                    {
                        options.ConnectionString = Configuration.GetValue<string>("Config:ConnectionString");
                        options.ApplicationName = "test";
                    });
    

    如果您想更细致地配置选项。

    【讨论】:

      猜你喜欢
      • 2018-06-27
      • 2014-01-23
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多