简体   繁体   English

从 appsettings.json 配置文件中读取

[英]Read from appsettings.json config file

I have the following config in my appsettings.json File我的 appsettings.json 文件中有以下配置

 {
  "AppSettings": {
    "MainUser": {
      "Name": "Urs",
      "Surname": "Barandun",
      "SesstionTimeInMin": "30",
    }
  },

And I'd like to create a custom configuration class and bind it.我想创建一个自定义配置类并绑定它。 Eg:例如:

MainUserConfiguration mainUserConfiguration = new MainUserConfiguration();
var configSection = config.GetSection("MainUserConfiguration");
configSection.Bind(mainUserConfiguration);

But, my code does not work.但是,我的代码不起作用。 :/ :/

Based on what you've provided, your appsettings file and the nested JSON for your configuration don't match up.根据您提供的内容,您的 appsettings 文件和配置的嵌套 JSON 不匹配。 A typical ASP.NET Core appsettings.json doesn't have a nested element called "AppSettings", and further you're getting a configuration section called "MainUserConfiguration" while your actual JSON just names that section "MainUser".典型的 ASP.NET Core appsettings.json没有名为“AppSettings”的嵌套元素,而且您会得到一个名为“MainUserConfiguration”的配置部分,而您的实际 JSON 只是将该部分命名为“MainUser”。

appsettings example:应用设置示例:

{
  "MyFirstClass": {
    "Option1": "some string value",
    "Option2": 42
  },
  "MySecondClass": {
    "SettingOne": "some string value",
    "SettingTwo": 42
  }
}

In your code:在您的代码中:

public class MyFirstClass
{
    public string Option1 { get; set; }
    public int Option2 { get; set; }
}

public class MySecondClass
{
    public string SettingOne { get; set; }
    public int SettingTwo { get; set; }
}

In your Startup.cs (presuming that's where you're accessing it with a defined Configuration object:在您的 Startup.cs 中(假设您使用定义的Configuration对象访问它:

var myFirstClass = Configuration.GetSection("MyFirstClass").Get<MyFirstClass>();
var mySecondClass = Configuration.GetSection("MySecondClass").Get<MySecondClass>();
Console.WriteLine($"The answer is always {myFirstClass.Option2}");

try this inside of startup在启动时试试这个

var appSettingsUserSection = configuration.GetSection("AppSettings:MainUser");

    services.Configure<MainUser>(appSettingsUserSection);

    var settings = appSettingsUserSection.Get<MainUserSettings>();
     var name=settings.Name; //Urs

class班级

public class MainUserSettings
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public int SesstionTimeInMin { get; set; }
}

now you can use it any class using code like this, in controller for example现在您可以使用这样的代码在任何类中使用它,例如在控制器中

public class MyController:Controller
    {
        private readonly IOptions<MainUserSettings> _userSettings;

        public MyController (IOptions<MainUserSettings> userSettings)
        {
            _userSettings = userSettings;
        }

        public  IActionResult MyAction()
        {
            var name=_userSettings.Name; //Urs
        }
}

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

相关问题 如何从appsettings.json中读取 - How to simply read from appsettings.json 由于 appsettings.json 的路径,add-migration 无法从文件 appsettings.json 读取连接字符串 - add-migration can not read the connection string from the file appsettings.json because of the path to appsettings.json 如何在 DI 设置期间自动验证 appSettings.json 文件中的配置值? - How to validate config values from appSettings.json file automatically during DI setup? 尝试从appsettings.json中读取Serilog配置 - Attempting to reading Serilog config from appsettings.json C# CommandLineApplication 控制台应用程序读取一个appsettings.json文件 - C# CommandLineApplication Console App to read an appsettings.json file 无法读取 dotnet 核心中链接的 appsettings.json 文件中的值 - Values from linked appsettings.json file in dotnet core cannot be read 从.NET Core 2中的类读取appsettings.json - Read appsettings.json from a class in .NET Core 2 无法从 appsettings.json 读取数据 - Can't read data from appsettings.json 如何从 .NET 核心中的 appsettings.json 读取部分值 - How to read section values from appsettings.json in .NET Core 在没有IOptions <T>的情况下从appsettings.json读取和使用设置? - Read and use settings from appsettings.json without IOptions<T>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM