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

inn6fuwd  于 2023-08-08  发布在  其他
关注(0)|答案(2)|浏览(113)

我想我的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://learn.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。

iyfjxgzm

iyfjxgzm1#

services.Configure<Config>(Configuration);

字符串
这一行没有达到预期的结果,因为您要查找的JSON属性嵌套在appsettings.json文件中的Config属性下。要按预期加载这些值,请使用GetSection获取Config部分并将 that 传递到Configure<TOptions>方法中:

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

0s7z1bwu

0s7z1bwu2#

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

字符串
如果您想更精细地配置选项。

相关问题