代码之家  ›  专栏  ›  技术社区  ›  Falcon

read appsettings.json-字段保持为空

  •  3
  • Falcon  · 技术社区  · 6 年前

    我觉得startup.cs有问题,因为我没有从我的 <IOption> config

    所以…我们有自己的 应用设置.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;
                }
    

    我尝试用类似

     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核心2.0

    enter image description here

    编辑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();
        }
    

    整个 应用设置.json 文件

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

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

    1 回复  |  直到 6 年前
        1
  •  4
  •   Kirk Larkin    6 年前

    services.Configure<Config>(Configuration) ;

    这一行没有达到预期的结果,因为您要查找的JSON属性嵌套在 Config 您的财产 appsettings.json 文件。为了加载这些值,需要使用 GetSection 攫取 配置 截面和孔型 那个 进入 Configure<TOptions> 方法:

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