ASP.NET Core—access Configuration from static class

后端 未结 13 2117
我在风中等你
我在风中等你 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:36

    Personally I like the method used in this link

    Essentially it just adding a static field to your options class.

     public class WeblogConfiguration
     {
        public static WeblogConfiguration Current;
    
        public WeblogConfiguration()
        {
            Current = this;
        }
    } 
    

    Then in any static class you can do:

    WeblogConfiguration.Current
    

    Simple and very straight forward

提交回复
热议问题