ASP.NET Core—access Configuration from static class

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

    A slightly shorter version based on the same principle as above...

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
        StaticConfig = configuration;
    }
    
    public static IConfiguration StaticConfig { get; private set; }
    

    To use in another static class:

    string connString = Startup.StaticConfig.GetConnectionString("DefaultConnection");
    

提交回复
热议问题