繁体   English   中英

读取 appsettings.json - 字段保持为空

[英]Read appsettings.json - Fields remain null

认为我的 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; }
    }

启动.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:

程序.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。 在此处输入图片说明

services.Configure<Config>(Configuration);

此行未达到预期结果,因为您要查找的 JSON 属性嵌套在appsettings.json文件中的Config属性下。 像预期的那样加载这些值,使用GetSection抓住Config部并传递Configure<TOptions>方法:

services.Configure<Config>(Configuration.GetSection("Config"));
     services.Configure<Config>(options =>
                {
                    options.ConnectionString = Configuration.GetValue<string>("Config:ConnectionString");
                    options.ApplicationName = "test";
                });

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM