ASP.NET Core—access Configuration from static class

后端 未结 13 1986
我在风中等你
我在风中等你 2021-02-01 00:27

I want a simple static class that accesses the Configuration object. All the config info is already read in from the appsettings.json file in the Startup class. I just need an e

13条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 00:39

    Consider using the instructions here for ASP.NET Core Configuration.

    You can create a class to store your configuration settings and then access the values, something like this:

    _config.UserName
    

    In Startup - ConfigureServices:

    services.Configure(Configuration.GetSections("General"));
    

    Then just inject your object wherever you need as:

    IOptions config
    

提交回复
热议问题